Advertisement
Guest User

Untitled

a guest
Jan 28th, 2012
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 19.02 KB | None | 0 0
  1. using System;
  2. using System.Globalization;
  3. using System.Reflection;
  4.  
  5. namespace fd.Base.Types
  6. {
  7.     /// <summary>A property info that can be used to pretend that a property is defined on a type when in fact it isn't.</summary>
  8.     public class GetterSetterPropertyInfo : PropertyInfo
  9.     {
  10.         private readonly Type _declaringType;
  11.         private readonly MethodInfo _getter;
  12.         private readonly string _propertyName;
  13.         private readonly Type _propertyType;
  14.         private readonly MethodInfo _setter;
  15.  
  16.         /// <summary>Initializes a new instance of the <see cref="GetterSetterPropertyInfo" /> class.</summary>
  17.         /// <param name="declaringType">The type this property pretends to be defined on.</param>
  18.         /// <param name="propertyType">The type of the property.</param>
  19.         /// <param name="propertyName">The name of the property.</param>
  20.         /// <param name="getter">The getter.</param>
  21.         /// <param name="setter">The setter.</param>
  22.         public GetterSetterPropertyInfo(Type declaringType, Type propertyType, string propertyName, MethodInfo getter, MethodInfo setter)
  23.         {
  24.             if (declaringType == null)
  25.                 throw new ArgumentNullException("declaringType");
  26.             if (propertyType == null)
  27.                 throw new ArgumentNullException("propertyType");
  28.             if (propertyName == null)
  29.                 throw new ArgumentNullException("propertyName");
  30.             if (getter == null && setter == null)
  31.                 throw new ArgumentException("At least one of the parameters setter and getter must be non-null.");
  32.  
  33.             _declaringType = declaringType;
  34.             _propertyType = propertyType;
  35.             _propertyName = propertyName;
  36.             _getter = getter;
  37.             _setter = setter;
  38.         }
  39.  
  40.         /// <summary>Gets the attributes for this property.</summary>
  41.         /// <returns><see cref="fd.Base.Types.GetterSetterPropertyInfo.Attributes" /> of this property.</returns>
  42.         public override PropertyAttributes Attributes
  43.         {
  44.             get { return PropertyAttributes.None; }
  45.         }
  46.  
  47.         /// <summary>Gets a value indicating whether the property can be read.</summary>
  48.         /// <returns><see langword="true" /> if this property can be read; otherwise, false.</returns>
  49.         public override bool CanRead
  50.         {
  51.             get { return _getter != null; }
  52.         }
  53.  
  54.         /// <summary>Gets a value indicating whether the property can be written to.</summary>
  55.         /// <returns><see langword="true" /> if this property can be written to; otherwise, false.</returns>
  56.         public override bool CanWrite
  57.         {
  58.             get { return _setter != null; }
  59.         }
  60.  
  61.         /// <summary>Gets the class that declares this member.</summary>
  62.         /// <returns>The Type object for the class that declares this member.</returns>
  63.         public override Type DeclaringType
  64.         {
  65.             get { return _declaringType; }
  66.         }
  67.  
  68.         /// <summary>Gets a value that identifies a metadata element.</summary>
  69.         /// <exception cref="InvalidOperationException">
  70.         /// The current <see cref="T:System.Reflection.MemberInfo" /> represents an array method, such as Address, on an array type whose element type is a
  71.         /// dynamic type that has not been completed. To get a metadata token in this case, pass the <see cref="MemberInfo" /> object to the
  72.         /// <see cref="System.Reflection.Emit.ModuleBuilder" /> method; or use the <see cref="System.Reflection.Emit.ModuleBuilder" /> method to get the token
  73.         /// directly, instead of using the <see cref="System.Reflection.Emit.ModuleBuilder" /> method to get a <see cref="MethodInfo" /> first.
  74.         /// </exception>
  75.         /// <returns>A value which, in combination with <see cref="System.Reflection.MemberInfo.Module" /> , uniquely identifies a metadata element.</returns>
  76.         public override int MetadataToken
  77.         {
  78.             get { return Name.GetHashCode(); }
  79.         }
  80.  
  81.         /// <summary>
  82.         /// Gets the module in which the type that declares the member represented by the current <see cref="T:System.Reflection.MemberInfo" /> is defined.
  83.         /// </summary>
  84.         /// <exception cref="T:System.NotImplementedException">This method is not implemented.</exception>
  85.         /// <returns>
  86.         /// The <see cref="T:System.Reflection.Module" /> in which the type that declares the member represented by the current
  87.         /// <see cref="T:System.Reflection.MemberInfo" /> is defined.
  88.         /// </returns>
  89.         public override Module Module
  90.         {
  91.             get { return _declaringType.Module; }
  92.         }
  93.  
  94.         /// <summary>Gets the name of the current member.</summary>
  95.         /// <returns>A <see cref="T:System.String" /> containing the name of this member.</returns>
  96.         public override string Name
  97.         {
  98.             get { return _propertyName; }
  99.         }
  100.  
  101.         /// <summary>Gets the type of this property.</summary>
  102.         /// <returns>The type of this property.</returns>
  103.         public override Type PropertyType
  104.         {
  105.             get { return _propertyType; }
  106.         }
  107.  
  108.         /// <summary>Gets the class object that was used to obtain this instance of MemberInfo.</summary>
  109.         /// <returns>The Type object through which this MemberInfo object was obtained.</returns>
  110.         public override Type ReflectedType
  111.         {
  112.             get { return _declaringType; }
  113.         }
  114.  
  115.         /// <summary>
  116.         /// Returns an array whose elements reflect the <see langword="public" /> and, if specified, non-public get, set, and other accessors of the property
  117.         /// reflected by the current instance.
  118.         /// </summary>
  119.         /// <param name="nonPublic">
  120.         /// Indicates whether non-public methods should be returned in the MethodInfo array. <see langword="true" /> if non-public methods are to be included;
  121.         /// otherwise, false.
  122.         /// </param>
  123.         /// <returns>
  124.         /// An array of <see cref="T:System.Reflection.MethodInfo" /> objects whose elements reflect the get, set, and other accessors of the property reflected
  125.         /// by the current instance. If <paramref name="nonPublic" /> is true, this array contains <see langword="public" /> and non-public get, set, and other
  126.         /// accessors. If <paramref name="nonPublic" /> is false, this array contains only <see langword="public" /> get, set, and other accessors. If no
  127.         /// accessors with the specified visibility are found, this method returns an array with zero (0) elements.
  128.         /// </returns>
  129.         public override MethodInfo[] GetAccessors(bool nonPublic)
  130.         {
  131.             if (_getter == null)
  132.                 return new[] { _setter };
  133.             else if (_setter == null)
  134.                 return new[] { _getter };
  135.             return new[] { _getter, _setter };
  136.         }
  137.  
  138.         /// <summary>When overridden in a derived class, returns an array containing all the custom attributes.</summary>
  139.         /// <param name="inherit">Specifies whether to search this member's inheritance chain to find the attributes.</param>
  140.         /// <exception cref="T:System.InvalidOperationException">
  141.         /// This member belongs to a type that is loaded into the reflection-only context. See How to: Load Assemblies into the Reflection-Only Context.
  142.         /// </exception>
  143.         /// <exception cref="T:System.TypeLoadException">A custom attribute type cannot be loaded.</exception>
  144.         /// <returns>An array that contains all the custom attributes, or an array with zero elements if no attributes are defined.</returns>
  145.         public override object[] GetCustomAttributes(bool inherit)
  146.         {
  147.             return new object[0];
  148.         }
  149.  
  150.         /// <summary>When overridden in a derived class, returns an array of custom attributes identified by <see cref="T:System.Type" /> .</summary>
  151.         /// <param name="attributeType">The type of attribute to search for. Only attributes that are assignable to this type are returned.</param>
  152.         /// <param name="inherit">Specifies whether to search this member's inheritance chain to find the attributes.</param>
  153.         /// <exception cref="T:System.TypeLoadException">A custom attribute type cannot be loaded.</exception>
  154.         /// <exception cref="T:System.ArgumentNullException">If <paramref name="attributeType" /> is null.</exception>
  155.         /// <exception cref="T:System.InvalidOperationException">
  156.         /// This member belongs to a type that is loaded into the reflection-only context. See How to: Load Assemblies into the Reflection-Only Context.
  157.         /// </exception>
  158.         /// <returns>An array of custom attributes applied to this member, or an array with zero (0) elements if no attributes have been applied.</returns>
  159.         public override object[] GetCustomAttributes(Type attributeType, bool inherit)
  160.         {
  161.             return new object[0];
  162.         }
  163.  
  164.         /// <summary>When overridden in a derived class, returns the <see langword="public" /> or non-public get accessor for this property.</summary>
  165.         /// <param name="nonPublic">
  166.         /// Indicates whether a non-public get accessor should be returned. <see langword="true" /> if a non-public accessor is to be returned; otherwise,
  167.         /// false.
  168.         /// </param>
  169.         /// <exception cref="T:System.Security.SecurityException">
  170.         /// The requested method is non-public and the caller does not have <see cref="T:System.Security.Permissions.ReflectionPermission" /> to reflect on this
  171.         /// non-public method.
  172.         /// </exception>
  173.         /// <returns>
  174.         /// A MethodInfo object representing the get accessor for this property, if <paramref name="nonPublic" /> is true. Returns <see langword="null" /> if
  175.         /// <paramref name="nonPublic" /> is <see langword="false" /> and the get accessor is non-public, or if <paramref name="nonPublic" /> is
  176.         /// <see langword="true" /> but no get accessors exist.
  177.         /// </returns>
  178.         public override MethodInfo GetGetMethod(bool nonPublic)
  179.         {
  180.             return _getter;
  181.         }
  182.  
  183.         /// <summary>When overridden in a derived class, returns an array of all the index parameters for the property.</summary>
  184.         /// <returns>An array of type ParameterInfo containing the parameters for the indexes.</returns>
  185.         public override ParameterInfo[] GetIndexParameters()
  186.         {
  187.             return new ParameterInfo[0];
  188.         }
  189.  
  190.         /// <summary>When overridden in a derived class, returns the set accessor for this property.</summary>
  191.         /// <param name="nonPublic">
  192.         /// Indicates whether the accessor should be returned if it is non-public. <see langword="true" /> if a non-public accessor is to be returned;
  193.         /// otherwise, false.
  194.         /// </param>
  195.         /// <exception cref="T:System.Security.SecurityException">
  196.         /// The requested method is non-public and the caller does not have <see cref="T:System.Security.Permissions.ReflectionPermission" /> to reflect on this
  197.         /// non-public method.
  198.         /// </exception>
  199.         /// <returns>
  200.         /// Value Condition A <see cref="T:System.Reflection.MethodInfo" /> object representing the Set method for this property. The set accessor is
  201.         /// public.-or- <paramref name="nonPublic" /> is <see langword="true" /> and the set accessor is non-public. <see langword="null" />
  202.         /// <paramref name="nonPublic" /> is true, but the property is read-only.-or- <paramref name="nonPublic" /> is <see langword="false" /> and the set
  203.         /// accessor is non-public.-or- There is no set accessor.
  204.         /// </returns>
  205.         public override MethodInfo GetSetMethod(bool nonPublic)
  206.         {
  207.             return _setter;
  208.         }
  209.  
  210.         /// <summary>
  211.         /// When overridden in a derived class, returns the value of a property having the specified binding, index, and
  212.         /// <see cref="T:System.Globalization.CultureInfo" /> .
  213.         /// </summary>
  214.         /// <param name="obj">The object whose property value will be returned.</param>
  215.         /// <param name="invokeAttr">
  216.         /// The invocation attribute. This must be a bit flag from BindingFlags : InvokeMethod, CreateInstance, Static, GetField, SetField, GetProperty, or
  217.         /// SetProperty. A suitable invocation attribute must be specified. If a <see langword="static" /> member is to be invoked, the Static flag of
  218.         /// BindingFlags must be set.
  219.         /// </param>
  220.         /// <param name="binder">
  221.         /// An object that enables the binding, coercion of argument types, invocation of members, and retrieval of MemberInfo objects via reflection. If
  222.         /// <paramref name="binder" /> is null, the default binder is used.
  223.         /// </param>
  224.         /// <param name="index">Optional index values for indexed properties. This value should be <see langword="null" /> for non-indexed properties.</param>
  225.         /// <param name="culture">
  226.         /// The CultureInfo object that represents the culture for which the resource is to be localized. Note that if the resource is not localized for this
  227.         /// culture, the CultureInfo.Parent method will be called successively in search of a match. If this value is null, the CultureInfo is obtained from the
  228.         /// CultureInfo.CurrentUICulture property.
  229.         /// </param>
  230.         /// <exception cref="T:System.ArgumentException">
  231.         /// The <paramref name="index" /> array does not contain the type of arguments needed.-or- The property's get accessor is not found.
  232.         /// </exception>
  233.         /// <exception cref="T:System.Reflection.TargetException">
  234.         /// The object does not match the target type, or a property is an instance property but <paramref name="obj" /> is null.
  235.         /// </exception>
  236.         /// <exception cref="T:System.Reflection.TargetParameterCountException">
  237.         /// The number of parameters in <paramref name="index" /> does not match the number of parameters the indexed property takes.
  238.         /// </exception>
  239.         /// <exception cref="T:System.MethodAccessException">
  240.         /// There was an illegal attempt to access a <see langword="private" /> or <see langword="protected" /> method inside a class.
  241.         /// </exception>
  242.         /// <exception cref="T:System.Reflection.TargetInvocationException">
  243.         /// An error occurred while retrieving the property value. For example, an <paramref name="index" /> value specified for an indexed property is out of
  244.         /// range. The <see cref="P:System.Exception.InnerException" /> property indicates the reason for the error.
  245.         /// </exception>
  246.         /// <returns>The property value for <paramref name="obj" /> .</returns>
  247.         public override object GetValue(object obj, BindingFlags invokeAttr, Binder binder, object[] index, CultureInfo culture)
  248.         {
  249.             if (_getter != null)
  250.                 return _getter.Invoke(obj, null);
  251.             throw new ArgumentException();
  252.         }
  253.  
  254.         /// <summary>
  255.         /// When overridden in a derived class, indicates whether one or more instance of <paramref name="attributeType" /> is applied to this member.
  256.         /// </summary>
  257.         /// <param name="attributeType">The Type object to which the custom attributes are applied.</param>
  258.         /// <param name="inherit">Specifies whether to search this member's inheritance chain to find the attributes.</param>
  259.         /// <returns><see langword="true" /> if one or more instance of <paramref name="attributeType" /> is applied to this member; otherwise false.</returns>
  260.         public override bool IsDefined(Type attributeType, bool inherit)
  261.         {
  262.             return false;
  263.         }
  264.  
  265.         /// <summary>When overridden in a derived class, sets the property <paramref name="value" /> for the given object to the given value.</summary>
  266.         /// <param name="obj">The object whose property <paramref name="value" /> will be set.</param>
  267.         /// <param name="value">The new value for this property.</param>
  268.         /// <param name="invokeAttr">
  269.         /// The invocation attribute. This must be a bit flag from <see cref="T:System.Reflection.BindingFlags" /> : InvokeMethod, CreateInstance, Static,
  270.         /// GetField, SetField, GetProperty, or SetProperty. A suitable invocation attribute must be specified. If a <see langword="static" /> member is to be
  271.         /// invoked, the Static flag of BindingFlags must be set.
  272.         /// </param>
  273.         /// <param name="binder">
  274.         /// An object that enables the binding, coercion of argument types, invocation of members, and retrieval of
  275.         /// <see cref="T:System.Reflection.MemberInfo" /> objects through reflection. If <paramref name="binder" /> is null, the default binder is used.
  276.         /// </param>
  277.         /// <param name="index">
  278.         /// Optional index values for indexed properties. This <paramref name="value" /> should be <see langword="null" /> for non-indexed properties.
  279.         /// </param>
  280.         /// <param name="culture">
  281.         /// The <see cref="T:System.Globalization.CultureInfo" /> object that represents the culture for which the resource is to be localized. Note that if the
  282.         /// resource is not localized for this culture, the CultureInfo.Parent method will be called successively in search of a match. If this
  283.         /// <paramref name="value" /> is null, the CultureInfo is obtained from the CultureInfo.CurrentUICulture property.
  284.         /// </param>
  285.         /// <exception cref="T:System.ArgumentException">
  286.         /// The <paramref name="index" /> array does not contain the type of arguments needed.-or- The property's set accessor is not found.
  287.         /// </exception>
  288.         /// <exception cref="T:System.Reflection.TargetException">
  289.         /// The object does not match the target type, or a property is an instance property but <paramref name="obj" /> is null.
  290.         /// </exception>
  291.         /// <exception cref="T:System.Reflection.TargetParameterCountException">
  292.         /// The number of parameters in <paramref name="index" /> does not match the number of parameters the indexed property takes.
  293.         /// </exception>
  294.         /// <exception cref="T:System.MethodAccessException">
  295.         /// There was an illegal attempt to access a <see langword="private" /> or <see langword="protected" /> method inside a class.
  296.         /// </exception>
  297.         /// <exception cref="T:System.Reflection.TargetInvocationException">
  298.         /// An error occurred while setting the property value. For example, an <paramref name="index" /> <paramref name="value" /> specified for an indexed
  299.         /// property is out of range. The <see cref="P:System.Exception.InnerException" /> property indicates the reason for the error.
  300.         /// </exception>
  301.         public override void SetValue(object obj, object value, BindingFlags invokeAttr, Binder binder, object[] index, CultureInfo culture)
  302.         {
  303.             if (_setter != null)
  304.                 _setter.Invoke(obj, new[] { value });
  305.             else
  306.                 throw new ArgumentException();
  307.         }
  308.     }
  309. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement