ParameterBuilder Class

Creates or associates parameter information.


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

<ClassInterfaceAttribute(ClassInterfaceType.None)> _
<ComVisibleAttribute(True)> _
Public Class ParameterBuilder

The ParameterBuilder type exposes the following members.

Public propertySupported by Silverlight for Windows PhoneAttributesGets the attributes for this parameter.
Public propertySupported by Silverlight for Windows PhoneIsInGets a value that indicates whether this is an input parameter.
Public propertySupported by Silverlight for Windows PhoneIsOptionalGets a value that indicates whether this parameter is optional.
Public propertySupported by Silverlight for Windows PhoneIsOutGets a value that indicates whether this parameter is an output parameter.
Public propertySupported by Silverlight for Windows PhoneNameGets the name of this parameter.
Public propertySupported by Silverlight for Windows PhonePositionGets the signature position for this parameter.

Public methodSupported by Silverlight for Windows PhoneEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows PhoneFinalizeAllows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodGetTokenGets the token for this parameter.
Public methodSupported by Silverlight for Windows PhoneGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows PhoneMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSetConstantSets the default value of the parameter.
Public methodSetCustomAttribute(CustomAttributeBuilder)Set a custom attribute using a custom attribute builder.
Public methodSupported by Silverlight for Windows PhoneSetCustomAttribute(ConstructorInfo, Byte())Security Critical. Set a custom attribute using a specified custom attribute blob.
Public methodSupported by Silverlight for Windows PhoneToStringReturns a string that represents the current object. (Inherited from Object.)

Parameter attributes need to consistent with the method signature. If you specify Out attributes for a parameter, you should ensure that the type of that method parameter is a ByRef type.

Some ParameterBuilder attributes require that you call DefineMethod with viable parameters in order for the Microsoft intermediate language (MSIL) to work correctly at runtime. For example, if you define a ParameterBuilder with ParameterAttributes.Out for parameter 1 of a MethodBuilder, then parameter 1 of MethodBuilder must be a reference such as Type.GetType("System.String&"), rather than Type.GetType("System.String").

The following example demonstrates how to create a dynamic method with a parameter passed by reference using ParameterBuilder.

Imports System.Threading
Imports System.Reflection
Imports System.Reflection.Emit


Class Example

   Public Shared Function BuildCustomerDataType() As Type

      Dim myDomain As AppDomain = Thread.GetDomain()
      Dim myAsmName As New AssemblyName()
      myAsmName.Name = "MyDynamicAssembly"

      Dim myAsmBuilder As AssemblyBuilder = myDomain.DefineDynamicAssembly(myAsmName, _

      Dim myModBuilder As ModuleBuilder = myAsmBuilder.DefineDynamicModule("MyMod")

      Dim myTypeBuilder As TypeBuilder = myModBuilder.DefineType("CustomerData", TypeAttributes.Public)

      Dim customerNameBldr As FieldBuilder = myTypeBuilder.DefineField("customerName", _
        GetType(String), _
      Dim acctIDBldr As FieldBuilder = myTypeBuilder.DefineField("acctID", _
        GetType(String), _
      Dim balanceAmtBldr As FieldBuilder = myTypeBuilder.DefineField("balanceAmt", _
        GetType(Double), _

      Dim myCtorBuilder As ConstructorBuilder = myTypeBuilder.DefineConstructor(MethodAttributes.Public, _
        CallingConventions.HasThis, _
        New Type() {GetType(String), _
             GetType(String), _

      Dim ctorIL As ILGenerator = myCtorBuilder.GetILGenerator()

      Dim objType As Type = Type.GetType("System.Object")
      Dim objCtor As ConstructorInfo = objType.GetConstructor(New Type() {})

      ctorIL.Emit(OpCodes.Call, objCtor)

      ctorIL.Emit(OpCodes.Stfld, customerNameBldr)

      ctorIL.Emit(OpCodes.Stfld, acctIDBldr)

      ctorIL.Emit(OpCodes.Stfld, balanceAmtBldr)


      ' This method will take an amount from a static pool and add it to the balance.
      ' Note that we are passing the first parameter, fundsPool, by reference. Therefore,
      ' we need to inform the MethodBuilder to expect a ref, by declaring the first
      ' parameter's type to be System.Double& (a reference to a double).

      Dim myMthdBuilder As MethodBuilder = myTypeBuilder.DefineMethod("AddFundsFromPool", _
        MethodAttributes.Public, _
        GetType(Double), _
        New Type() {Type.GetType("System.Double&"), _

      Dim poolRefBuilder As ParameterBuilder = myMthdBuilder.DefineParameter(1, _
       ParameterAttributes.Out, "fundsPool")

      Dim amountFromPoolBuilder As ParameterBuilder = myMthdBuilder.DefineParameter(2, _
       ParameterAttributes.In, "amountFromPool")

      Dim mthdIL As ILGenerator = myMthdBuilder.GetILGenerator()



      mthdIL.Emit(OpCodes.Ldfld, balanceAmtBldr)

      mthdIL.Emit(OpCodes.Stfld, balanceAmtBldr)

      mthdIL.Emit(OpCodes.Ldfld, balanceAmtBldr)

      Return myTypeBuilder.CreateType()

   End Function 'BuildCustomerDataType

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

      Dim custType As Type = Nothing
      Dim custObj As Object = Nothing

      Dim custArgTypes() As Type = {GetType(String), GetType(String), GetType(Double)}

      ' Call the method to build our dynamic class.
      custType = BuildCustomerDataType()

      outputBlock.Text &= "---" & vbCrLf

      Dim myCustCtor As ConstructorInfo = custType.GetConstructor(custArgTypes)
      Dim initialBalance As Double = 100.0
      custObj = myCustCtor.Invoke(New Object() {"Joe Consumer", "5678-XYZ", initialBalance})

      Dim myMemberInfo As MemberInfo() = custType.GetMember("AddFundsFromPool")

      Dim thePool As Double = 1000.0
      outputBlock.Text &= String.Format("The pool is currently ${0}", thePool) & vbCrLf
      outputBlock.Text &= String.Format("The original balance of the account instance is ${0}", initialBalance) & vbCrLf

      Dim amountFromPool As Double = 50.0
      outputBlock.Text &= String.Format("The amount to be subtracted from the pool and added " & _
   "to the account is ${0}", amountFromPool) & vbCrLf

      outputBlock.Text &= "---" & vbCrLf
      outputBlock.Text &= String.Format("Calling {0} ...", myMemberInfo(0).ToString()) & vbCrLf
      outputBlock.Text &= "---" & vbCrLf

      Dim passMe() As Object = {thePool, amountFromPool}
      outputBlock.Text &= String.Format("The new balance in the account instance is ${0}", _
    custType.InvokeMember("AddFundsFromPool", _
    BindingFlags.InvokeMethod, Nothing, custObj, passMe)) & vbCrLf
      thePool = CDbl(passMe(0))
      outputBlock.Text &= String.Format("The new amount in the pool is ${0}", thePool) & vbCrLf

   End Sub 'Main 

End Class 'ParamBuilderDemo


Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1

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.

Community Additions