PropertyBuilder.SetGetMethod Method

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

Sets a get accessor, which is the method that gets the property value.

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

Syntax

'Declaration
<SecuritySafeCriticalAttribute> _
Public Sub SetGetMethod ( _
    mdBuilder As MethodBuilder _
)
[SecuritySafeCriticalAttribute]
public void SetGetMethod(
    MethodBuilder mdBuilder
)

Parameters

Exceptions

Exception Condition
ArgumentNullException

mdBuilder is nulla null reference (Nothing in Visual Basic).

InvalidOperationException

CreateType has been called on the enclosing type.

Examples

The following example demonstrates how to define a property, create a set accessor, and attach the get accessor to the property by using the SetGetMethod 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 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.