次の方法で共有


PropertyBuilder Class

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Defines a property for a dynamic type.

Inheritance Hierarchy

System.Object
  System.Reflection.MemberInfo
    System.Reflection.PropertyInfo
      System.Reflection.Emit.PropertyBuilder

Namespace:  System.Reflection.Emit
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
<ComVisibleAttribute(True)> _
<ClassInterfaceAttribute(ClassInterfaceType.None)> _
Public NotInheritable Class PropertyBuilder _
    Inherits PropertyInfo
[ComVisibleAttribute(true)]
[ClassInterfaceAttribute(ClassInterfaceType.None)]
public sealed class PropertyBuilder : PropertyInfo

The PropertyBuilder type exposes the following members.

Properties

  Name Description
Public property Attributes Gets the attributes for this property. (Overrides PropertyInfo.Attributes.)
Public property CanRead Gets a value indicating whether the property has a get accessor. (Overrides PropertyInfo.CanRead.)
Public property CanWrite Gets a value indicating whether the property has a set accessor. (Overrides PropertyInfo.CanWrite.)
Public property DeclaringType Gets the class that declares this member. (Overrides MemberInfo.DeclaringType.)
Public property IsSpecialName Gets a value indicating whether the name of the property is recognized as a special name by compilers. (Inherited from PropertyInfo.)
Public property MemberType Gets a MemberTypes value indicating that this member is a property. (Inherited from PropertyInfo.)
Public property MetadataToken Gets a value that identifies a metadata element. (Inherited from MemberInfo.)
Public property Module Gets the module in which the type that declares the current property is being defined. (Overrides MemberInfo.Module.)
Public property Name Gets the name of this member. (Overrides MemberInfo.Name.)
Public property PropertyToken Gets the token for this property.
Public property PropertyType Gets the type of the property. (Overrides PropertyInfo.PropertyType.)
Public property ReflectedType Gets the dynamic type on which this property is defined. (Overrides MemberInfo.ReflectedType.)

Top

Methods

  Name Description
Public method AddOtherMethod Adds one of the methods other than property accessors that can be associated with a property.
Public method Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public method GetAccessors() Returns an array whose elements reflect the public get, set, and other accessors of the property reflected by the current instance. (Inherited from PropertyInfo.)
Public method GetAccessors(Boolean) Returns an array of the public and non-public get and set accessors on this property. This method is not supported. (Overrides PropertyInfo.GetAccessors(Boolean).)
Public method GetConstantValue Returns a literal value associated with the property by a compiler. (Inherited from PropertyInfo.)
Public method GetCustomAttributes(Boolean) Returns an array of all the custom attributes for this property. This method is not supported. (Overrides MemberInfo.GetCustomAttributes(Boolean).)
Public method GetCustomAttributes(Type, Boolean) Returns an array of custom attributes identified by Type. This method is not supported. (Overrides MemberInfo.GetCustomAttributes(Type, Boolean).)
Public method GetGetMethod() Returns the public get accessor for this property. (Inherited from PropertyInfo.)
Public method GetGetMethod(Boolean) Returns the public or non-public get accessor for this property. (Overrides PropertyInfo.GetGetMethod(Boolean).)
Public method GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method GetIndexParameters Returns an array of all the index parameters for the property. This method is not supported. (Overrides PropertyInfo.GetIndexParameters().)
Public method GetRawConstantValue Returns a literal value associated with the property by a compiler. (Inherited from PropertyInfo.)
Public method GetSetMethod() Returns the public set accessor for this property. (Inherited from PropertyInfo.)
Public method GetSetMethod(Boolean) Returns the set accessor for this property. (Overrides PropertyInfo.GetSetMethod(Boolean).)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Public method GetValue(Object, array<Object[]) Gets the value of the indexed property by calling the property's getter method. This method is not supported. (Overrides PropertyInfo.GetValue(Object, array<Object[]).)
Public method GetValue(Object, BindingFlags, Binder, array<Object[], CultureInfo) Gets the value of a property that has the specified binding, index, and CultureInfo. This method is not supported. (Overrides PropertyInfo.GetValue(Object, BindingFlags, Binder, array<Object[], CultureInfo).)
Public method IsDefined Indicates whether one or more instances of attributeType are defined on this property. This method is not supported. (Overrides MemberInfo.IsDefined(Type, Boolean).)
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method SetConstant Sets the default value of this property.
Public method SetCustomAttribute Applies a custom attribute by using a custom attribute builder.
Public method SetGetMethod Sets a get accessor, which is the method that gets the property value.
Public method SetSetMethod Sets the set accessor, which is the method that sets the property value.
Public method SetValue(Object, Object, array<Object[]) Sets the value of the property with optional index values for index properties. This method is not supported. (Overrides PropertyInfo.SetValue(Object, Object, array<Object[]).)
Public method SetValue(Object, Object, BindingFlags, Binder, array<Object[], CultureInfo) Sets the property value for the given object to the given value. This method is not supported. (Overrides PropertyInfo.SetValue(Object, Object, BindingFlags, Binder, array<Object[], CultureInfo).)
Public method ToString Returns a string that represents the current object. (Inherited from Object.)

Top

Remarks

A PropertyBuilder is always associated with a TypeBuilder. To get a PropertyBuilder, use the TypeBuilder.DefineProperty method.

Examples

The following example demonstrates how to implement a property in a dynamic type. The example defines a property named Number that gets and sets the value of a private field named m_number. The example defines get and set accessor methods that have the proper attributes and names consistent with the conventions used by the C# and Visual Basic compilers. The example generates method bodies for the accessors, and assigns the accessors to the property.

This example is part of a larger example provided for the TypeBuilder class.

' Define a property named Number that gets and sets the private 
' field.
'
' The last argument of DefineProperty is Nothing, because the
' property has no parameters. (If you don't specify Nothing, you must
' specify an array of Type objects. For a parameterless property,
' use the built-in array with no elements: Type.EmptyTypes)
Dim pbNumber As PropertyBuilder = tb.DefineProperty( _
    "Number", _
    PropertyAttributes.HasDefault, _
    GetType(Integer), _
    Nothing)

' The property Set and property Get methods require a special
' set of attributes.
Dim getSetAttr As MethodAttributes = _
    MethodAttributes.Public Or MethodAttributes.SpecialName _
        Or MethodAttributes.HideBySig

' Define the "get" accessor method for Number. The method returns
' an integer and has no arguments. (Note that Nothing could be 
' used instead of Types.EmptyTypes)
Dim mbNumberGetAccessor As MethodBuilder = tb.DefineMethod( _
    "get_Number", _
    getSetAttr, _
    GetType(Integer), _
    Type.EmptyTypes)

Dim numberGetIL As ILGenerator = mbNumberGetAccessor.GetILGenerator()
' For an instance property, argument zero is the instance. Load the 
' instance, then load the private field and return, leaving the
' field value on the stack.
numberGetIL.Emit(OpCodes.Ldarg_0)
numberGetIL.Emit(OpCodes.Ldfld, fbNumber)
numberGetIL.Emit(OpCodes.Ret)

' Define the "set" accessor method for Number, which has no return
' type and takes one argument of type Integer (Int32).
Dim mbNumberSetAccessor As MethodBuilder = _
    tb.DefineMethod( _
        "set_Number", _
        getSetAttr, _
        Nothing, _
        New Type() {GetType(Integer)})

Dim numberSetIL As ILGenerator = mbNumberSetAccessor.GetILGenerator()
' Load the instance and then the numeric argument, then store the
' argument in the field.
numberSetIL.Emit(OpCodes.Ldarg_0)
numberSetIL.Emit(OpCodes.Ldarg_1)
numberSetIL.Emit(OpCodes.Stfld, fbNumber)
numberSetIL.Emit(OpCodes.Ret)

' Last, map the "get" and "set" accessor methods to the 
' PropertyBuilder. The property is now complete. 
pbNumber.SetGetMethod(mbNumberGetAccessor)
pbNumber.SetSetMethod(mbNumberSetAccessor)
// Define a property named Number that gets and sets the private 
// field.
//
// The last argument of DefineProperty is null, because the
// property has no parameters. (If you don't specify null, you must
// specify an array of Type objects. For a parameterless property,
// use the built-in array with no elements: Type.EmptyTypes)
PropertyBuilder pbNumber = tb.DefineProperty(
    "Number",
    PropertyAttributes.HasDefault,
    typeof(int),
    null);

// The property "set" and property "get" methods require a special
// set of attributes.
MethodAttributes getSetAttr = MethodAttributes.Public |
    MethodAttributes.SpecialName | MethodAttributes.HideBySig;

// Define the "get" accessor method for Number. The method returns
// an integer and has no arguments. (Note that null could be 
// used instead of Types.EmptyTypes)
MethodBuilder mbNumberGetAccessor = tb.DefineMethod(
    "get_Number",
    getSetAttr,
    typeof(int),
    Type.EmptyTypes);

ILGenerator numberGetIL = mbNumberGetAccessor.GetILGenerator();
// For an instance property, argument zero is the instance. Load the 
// instance, then load the private field and return, leaving the
// field value on the stack.
numberGetIL.Emit(OpCodes.Ldarg_0);
numberGetIL.Emit(OpCodes.Ldfld, fbNumber);
numberGetIL.Emit(OpCodes.Ret);

// Define the "set" accessor method for Number, which has no return
// type and takes one argument of type int (Int32).
MethodBuilder mbNumberSetAccessor = tb.DefineMethod(
    "set_Number",
    getSetAttr,
    null,
    new Type[] { typeof(int) });

ILGenerator numberSetIL = mbNumberSetAccessor.GetILGenerator();
// Load the instance and then the numeric argument, then store the
// argument in the field.
numberSetIL.Emit(OpCodes.Ldarg_0);
numberSetIL.Emit(OpCodes.Ldarg_1);
numberSetIL.Emit(OpCodes.Stfld, fbNumber);
numberSetIL.Emit(OpCodes.Ret);

// Last, map the "get" and "set" accessor methods to the 
// PropertyBuilder. The property is now complete. 
pbNumber.SetGetMethod(mbNumberGetAccessor);
pbNumber.SetSetMethod(mbNumberSetAccessor);

Version Information

Silverlight

Supported in: 5, 4, 3

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.