Exporter (0) Imprimer
Développer tout

PropertyBuilder Class

Defines a property for a dynamic type.

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

[ComVisibleAttribute(true)]
[ClassInterfaceAttribute(ClassInterfaceType.None)]
public sealed class PropertyBuilder : PropertyInfo

The PropertyBuilder type exposes the following members.

  NameDescription
Public propertyAttributesGets the attributes for this property. (Overrides PropertyInfo.Attributes.)
Public propertyCanReadGets a value indicating whether the property has a get accessor. (Overrides PropertyInfo.CanRead.)
Public propertyCanWriteGets a value indicating whether the property has a set accessor. (Overrides PropertyInfo.CanWrite.)
Public propertyDeclaringTypeGets the class that declares this member. (Overrides MemberInfo.DeclaringType.)
Public propertyIsSpecialNameGets a value indicating whether the name of the property is recognized as a special name by compilers. (Inherited from PropertyInfo.)
Public propertyMemberTypeGets a MemberTypes value indicating that this member is a property. (Inherited from PropertyInfo.)
Public propertyMetadataTokenGets a value that identifies a metadata element. (Inherited from MemberInfo.)
Public propertyModuleGets the module in which the type that declares the current property is being defined. (Overrides MemberInfo.Module.)
Public propertyNameGets the name of this member. (Overrides MemberInfo.Name.)
Public propertyPropertyTokenGets the token for this property.
Public propertyPropertyTypeGets the type of the property. (Overrides PropertyInfo.PropertyType.)
Public propertyReflectedTypeGets the dynamic type on which this property is defined. (Overrides MemberInfo.ReflectedType.)
Top

  NameDescription
Public methodAddOtherMethodAdds one of the methods other than property accessors that can be associated with a property.
Public methodEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetAccessors()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 methodGetAccessors(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 methodGetConstantValueReturns a literal value associated with the property by a compiler. (Inherited from PropertyInfo.)
Public methodGetCustomAttributes(Boolean)Returns an array of all the custom attributes for this property. This method is not supported. (Overrides MemberInfo.GetCustomAttributes(Boolean).)
Public methodGetCustomAttributes(Type, Boolean)Returns an array of custom attributes identified by Type. This method is not supported. (Overrides MemberInfo.GetCustomAttributes(Type, Boolean).)
Public methodGetGetMethod()Returns the public get accessor for this property. (Inherited from PropertyInfo.)
Public methodGetGetMethod(Boolean)Returns the public or non-public get accessor for this property. (Overrides PropertyInfo.GetGetMethod(Boolean).)
Public methodGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodGetIndexParametersReturns an array of all the index parameters for the property. This method is not supported. (Overrides PropertyInfo.GetIndexParameters().)
Public methodGetRawConstantValueReturns a literal value associated with the property by a compiler. (Inherited from PropertyInfo.)
Public methodGetSetMethod()Returns the public set accessor for this property. (Inherited from PropertyInfo.)
Public methodGetSetMethod(Boolean)Returns the set accessor for this property. (Overrides PropertyInfo.GetSetMethod(Boolean).)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodGetValue(Object, Object[])Gets the value of the indexed property by calling the property's getter method. This method is not supported. (Overrides PropertyInfo.GetValue(Object, Object[]).)
Public methodGetValue(Object, BindingFlags, Binder, 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, Object[], CultureInfo).)
Public methodIsDefinedIndicates whether one or more instances of attributeType are defined on this property. This method is not supported. (Overrides MemberInfo.IsDefined(Type, Boolean).)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodSetConstantSets the default value of this property.
Public methodSetCustomAttributeApplies a custom attribute by using a custom attribute builder.
Public methodSetGetMethodSets a get accessor, which is the method that gets the property value.
Public methodSetSetMethodSets the set accessor, which is the method that sets the property value.
Public methodSetValue(Object, Object, Object[])Sets the value of the property with optional index values for index properties. This method is not supported. (Overrides PropertyInfo.SetValue(Object, Object, Object[]).)
Public methodSetValue(Object, Object, BindingFlags, Binder, 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, Object[], CultureInfo).)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

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

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 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);


Silverlight

Supported in: 5, 4, 3

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

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

Ajouts de la communauté

AJOUTER
Afficher:
© 2014 Microsoft