Export (0) Print
Expand All

PropertyBuilder.SetSetMethod Method

Sets the set accessor, which is the method that sets the property value.

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

[SecuritySafeCriticalAttribute]
public void SetSetMethod(
	MethodBuilder mdBuilder
)

Parameters

mdBuilder
Type: System.Reflection.Emit.MethodBuilder
The method that sets the property value.

ExceptionCondition
ArgumentNullException

mdBuilder is null.

InvalidOperationException

CreateType has been called on the enclosing type.

The following example demonstrates how to define a property, create a set accessor, and attach the set accessor to the property by using the SetSetMethod method.

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.

Community Additions

ADD
Show:
© 2014 Microsoft