The Labs \ Source Viewer \ SSCLI \ System.Reflection.Emit \ PropertyBuilder

  1. // ==++==
  2. //
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. //
  14. // ==--==
  15. /*============================================================
  16. **
  17. ** Class:  PropertyBuilder
  18. **
  19. **
  20. ** Propertybuilder is for client to define properties for a class
  21. **
  22. **
  23. ===========================================================*/
  24. namespace System.Reflection.Emit
  25. {
  26.    
  27.     using System;
  28.     using System.Reflection;
  29.     using CultureInfo = System.Globalization.CultureInfo;
  30.     using System.Security.Permissions;
  31.     using System.Runtime.InteropServices;
  32.    
  33.     //
  34.     // A PropertyBuilder is always associated with a TypeBuilder. The TypeBuilder.DefineProperty
  35.     // method will return a new PropertyBuilder to a client.
  36.     //
  37.     [HostProtection(MayLeakOnAbort = true)]
  38.     [ClassInterface(ClassInterfaceType.None)]
  39.     [ComDefaultInterface(typeof(_PropertyBuilder))]
  40.     [System.Runtime.InteropServices.ComVisible(true)]
  41.     public sealed class PropertyBuilder : PropertyInfo, _PropertyBuilder
  42.     {
  43.        
  44.         // Make a private constructor so these cannot be constructed externally.
  45.         private PropertyBuilder()
  46.         {
  47.         }
  48.        
  49.         // Constructs a PropertyBuilder.
  50.         //
  51.             // the module containing this PropertyBuilder
  52.             // property name
  53.             // property signature descriptor info
  54.             // property attribute such as DefaultProperty, Bindable, DisplayBind, etc
  55.             // return type of the property.
  56.             // the metadata token for this property
  57.         // the containing type
  58.         internal PropertyBuilder(Module mod, string name, SignatureHelper sig, PropertyAttributes attr, Type returnType, PropertyToken prToken, TypeBuilder containingType)
  59.         {
  60.             if (name == null)
  61.                 throw new ArgumentNullException("name");
  62.             if (name.Length == 0)
  63.                 throw new ArgumentException(Environment.GetResourceString("Argument_EmptyName"), "name");
  64.             if (name[0] == '\0')
  65.                 throw new ArgumentException(Environment.GetResourceString("Argument_IllegalName"), "name");
  66.            
  67.             m_name = name;
  68.             m_module = mod;
  69.             m_signature = sig;
  70.             m_attributes = attr;
  71.             m_returnType = returnType;
  72.             m_prToken = prToken;
  73.             m_tkProperty = prToken.Token;
  74.             m_getMethod = null;
  75.             m_setMethod = null;
  76.             m_containingType = containingType;
  77.         }
  78.        
  79.         //************************************************
  80.         // Set the default value of the Property
  81.         //************************************************
  82.         public void SetConstant(object defaultValue)
  83.         {
  84.             m_containingType.ThrowIfCreated();
  85.            
  86.             TypeBuilder.SetConstantValue(m_module, m_prToken.Token, m_returnType, defaultValue);
  87.         }
  88.        
  89.        
  90.         // Return the Token for this property within the TypeBuilder that the
  91.         // property is defined within.
  92.         public PropertyToken PropertyToken {
  93.             get { return m_prToken; }
  94.         }
  95.        
  96.         internal override int MetadataTokenInternal {
  97.             get { return m_tkProperty; }
  98.         }
  99.        
  100.         public override Module Module {
  101.             get { return m_containingType.Module; }
  102.         }
  103.        
  104.        
  105.         public void SetGetMethod(MethodBuilder mdBuilder)
  106.         {
  107.             if (mdBuilder == null) {
  108.                 throw new ArgumentNullException("mdBuilder");
  109.             }
  110.             m_containingType.ThrowIfCreated();
  111.             TypeBuilder.InternalDefineMethodSemantics(m_module, m_prToken.Token, MethodSemanticsAttributes.Getter, mdBuilder.GetToken().Token);
  112.             m_getMethod = mdBuilder;
  113.         }
  114.        
  115.        
  116.         public void SetSetMethod(MethodBuilder mdBuilder)
  117.         {
  118.             if (mdBuilder == null) {
  119.                 throw new ArgumentNullException("mdBuilder");
  120.             }
  121.             m_containingType.ThrowIfCreated();
  122.             TypeBuilder.InternalDefineMethodSemantics(m_module, m_prToken.Token, MethodSemanticsAttributes.Setter, mdBuilder.GetToken().Token);
  123.             m_setMethod = mdBuilder;
  124.         }
  125.        
  126.         public void AddOtherMethod(MethodBuilder mdBuilder)
  127.         {
  128.             if (mdBuilder == null) {
  129.                 throw new ArgumentNullException("mdBuilder");
  130.             }
  131.             m_containingType.ThrowIfCreated();
  132.             TypeBuilder.InternalDefineMethodSemantics(m_module, m_prToken.Token, MethodSemanticsAttributes.Other, mdBuilder.GetToken().Token);
  133.         }
  134.        
  135.         // Use this function if client decides to form the custom attribute blob themselves
  136.         [System.Runtime.InteropServices.ComVisible(true)]
  137.         public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute)
  138.         {
  139.             if (con == null)
  140.                 throw new ArgumentNullException("con");
  141.             if (binaryAttribute == null)
  142.                 throw new ArgumentNullException("binaryAttribute");
  143.            
  144.             m_containingType.ThrowIfCreated();
  145.             TypeBuilder.InternalCreateCustomAttribute(m_prToken.Token, ((ModuleBuilder)m_module).GetConstructorToken(con).Token, binaryAttribute, m_module, false);
  146.         }
  147.        
  148.         // Use this function if client wishes to build CustomAttribute using CustomAttributeBuilder
  149.         public void SetCustomAttribute(CustomAttributeBuilder customBuilder)
  150.         {
  151.             if (customBuilder == null) {
  152.                 throw new ArgumentNullException("customBuilder");
  153.             }
  154.             m_containingType.ThrowIfCreated();
  155.             customBuilder.CreateCustomAttribute((ModuleBuilder)m_module, m_prToken.Token);
  156.         }
  157.        
  158.         // Not supported functions in dynamic module.
  159.         public override object GetValue(object obj, object[] index)
  160.         {
  161.             throw new NotSupportedException(Environment.GetResourceString("NotSupported_DynamicModule"));
  162.         }
  163.        
  164.         public override object GetValue(object obj, BindingFlags invokeAttr, Binder binder, object[] index, CultureInfo culture)
  165.         {
  166.             throw new NotSupportedException(Environment.GetResourceString("NotSupported_DynamicModule"));
  167.         }
  168.        
  169.         public override void SetValue(object obj, object value, object[] index)
  170.         {
  171.             throw new NotSupportedException(Environment.GetResourceString("NotSupported_DynamicModule"));
  172.         }
  173.        
  174.         public override void SetValue(object obj, object value, BindingFlags invokeAttr, Binder binder, object[] index, CultureInfo culture)
  175.         {
  176.             throw new NotSupportedException(Environment.GetResourceString("NotSupported_DynamicModule"));
  177.         }
  178.        
  179.         public override MethodInfo[] GetAccessors(bool nonPublic)
  180.         {
  181.             throw new NotSupportedException(Environment.GetResourceString("NotSupported_DynamicModule"));
  182.         }
  183.        
  184.         public override MethodInfo GetGetMethod(bool nonPublic)
  185.         {
  186.             if (nonPublic || m_getMethod == null)
  187.                 return m_getMethod;
  188.             // now check to see if m_getMethod is public
  189.             if ((m_getMethod.Attributes & MethodAttributes.Public) == MethodAttributes.Public)
  190.                 return m_getMethod;
  191.             return null;
  192.         }
  193.        
  194.         public override MethodInfo GetSetMethod(bool nonPublic)
  195.         {
  196.             if (nonPublic || m_setMethod == null)
  197.                 return m_setMethod;
  198.             // now check to see if m_setMethod is public
  199.             if ((m_setMethod.Attributes & MethodAttributes.Public) == MethodAttributes.Public)
  200.                 return m_setMethod;
  201.             return null;
  202.         }
  203.        
  204.         public override ParameterInfo[] GetIndexParameters()
  205.         {
  206.             throw new NotSupportedException(Environment.GetResourceString("NotSupported_DynamicModule"));
  207.         }
  208.        
  209.         public override Type PropertyType {
  210.             get { return m_returnType; }
  211.         }
  212.        
  213.         public override PropertyAttributes Attributes {
  214.             get { return m_attributes; }
  215.         }
  216.        
  217.         public override bool CanRead {
  218.             get {
  219.                 if (m_getMethod != null)
  220.                     return true;
  221.                 else
  222.                     return false;
  223.             }
  224.         }
  225.        
  226.         public override bool CanWrite {
  227.             get {
  228.                 if (m_setMethod != null)
  229.                     return true;
  230.                 else
  231.                     return false;
  232.             }
  233.         }
  234.        
  235.         public override object[] GetCustomAttributes(bool inherit)
  236.         {
  237.             throw new NotSupportedException(Environment.GetResourceString("NotSupported_DynamicModule"));
  238.         }
  239.        
  240.         public override object[] GetCustomAttributes(Type attributeType, bool inherit)
  241.         {
  242.             throw new NotSupportedException(Environment.GetResourceString("NotSupported_DynamicModule"));
  243.         }
  244.        
  245.         public override bool IsDefined(Type attributeType, bool inherit)
  246.         {
  247.             throw new NotSupportedException(Environment.GetResourceString("NotSupported_DynamicModule"));
  248.         }
  249.        
  250.         void _PropertyBuilder.GetTypeInfoCount(out uint pcTInfo)
  251.         {
  252.             throw new NotImplementedException();
  253.         }
  254.        
  255.         void _PropertyBuilder.GetTypeInfo(uint iTInfo, uint lcid, IntPtr ppTInfo)
  256.         {
  257.             throw new NotImplementedException();
  258.         }
  259.        
  260.         void _PropertyBuilder.GetIDsOfNames(        [In()]
  261. ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
  262.         {
  263.             throw new NotImplementedException();
  264.         }
  265.        
  266.         void _PropertyBuilder.Invoke(uint dispIdMember,         [In()]
  267. ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
  268.         {
  269.             throw new NotImplementedException();
  270.         }
  271.        
  272.        
  273.         public override string Name {
  274.             get { return m_name; }
  275.         }
  276.        
  277.         public override Type DeclaringType {
  278.             get { return m_containingType; }
  279.         }
  280.        
  281.         public override Type ReflectedType {
  282.             get { return m_containingType; }
  283.         }
  284.        
  285.         // These are package private so that TypeBuilder can access them.
  286.         private string m_name;
  287.         // The name of the property
  288.         private PropertyToken m_prToken;
  289.         // The token of this property
  290.         private int m_tkProperty;
  291.         private Module m_module;
  292.         private SignatureHelper m_signature;
  293.         private PropertyAttributes m_attributes;
  294.         // property's attribute flags
  295.         private Type m_returnType;
  296.         // property's return type
  297.         private MethodInfo m_getMethod;
  298.         private MethodInfo m_setMethod;
  299.         private TypeBuilder m_containingType;
  300.     }
  301.    
  302. }

Developer Fusion