ParameterBuilder Class

July 28, 2014

Creates or associates parameter information.

System.Object
  System.Reflection.Emit.ParameterBuilder

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

public class ParameterBuilder

The ParameterBuilder type exposes the following members.

  NameDescription
Public propertyAttributesGets the attributes for this parameter.
Public propertyIsInGets a value that indicates whether this is an input parameter.
Public propertyIsOptionalGets a value that indicates whether this parameter is optional.
Public propertyIsOutGets a value that indicates whether this parameter is an output parameter.
Public propertyNameGets the name of this parameter.
Public propertyPositionGets the signature position for this parameter.
Top

  NameDescription
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 methodGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodSetConstantSets the default value of the parameter.
Public methodSetCustomAttribute(CustomAttributeBuilder)Set a custom attribute using a custom attribute builder.
Public methodSetCustomAttribute(ConstructorInfo, Byte[])Set a custom attribute using a specified custom attribute blob.
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

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 provide 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 method, then parameter 1 of the method 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.



using System;
using System.Threading;
using System.Reflection;
using System.Reflection.Emit;

class Example
{

   public static Type BuildCustomerDataType()
   {

      AppDomain myDomain = Thread.GetDomain();
      AssemblyName myAsmName = new AssemblyName();
      myAsmName.Name = "MyDynamicAssembly";

      AssemblyBuilder myAsmBuilder = myDomain.DefineDynamicAssembly(myAsmName,
                     AssemblyBuilderAccess.Run);

      ModuleBuilder myModBuilder = myAsmBuilder.DefineDynamicModule("MyMod");

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

      FieldBuilder customerNameBldr = myTypeBuilder.DefineField("customerName",
                        typeof(string),
                        FieldAttributes.Private);
      FieldBuilder acctIDBldr = myTypeBuilder.DefineField("acctID",
                        typeof(string),
                        FieldAttributes.Private);
      FieldBuilder balanceAmtBldr = myTypeBuilder.DefineField("balanceAmt",
                        typeof(double),
                        FieldAttributes.Private);

      ConstructorBuilder myCtorBuilder = myTypeBuilder.DefineConstructor(
                        MethodAttributes.Public,
                        CallingConventions.HasThis,
                        new Type[] { typeof(string),
								     typeof(string),
								     typeof(double) });


      ILGenerator ctorIL = myCtorBuilder.GetILGenerator();

      Type objType = Type.GetType("System.Object");
      ConstructorInfo objCtor = objType.GetConstructor(new Type[] { });

      ctorIL.Emit(OpCodes.Ldarg_0);
      ctorIL.Emit(OpCodes.Call, objCtor);

      ctorIL.Emit(OpCodes.Ldarg_0);
      ctorIL.Emit(OpCodes.Ldarg_1);
      ctorIL.Emit(OpCodes.Stfld, customerNameBldr);

      ctorIL.Emit(OpCodes.Ldarg_0);
      ctorIL.Emit(OpCodes.Ldarg_2);
      ctorIL.Emit(OpCodes.Stfld, acctIDBldr);

      ctorIL.Emit(OpCodes.Ldarg_0);
      ctorIL.Emit(OpCodes.Ldarg_3);
      ctorIL.Emit(OpCodes.Stfld, balanceAmtBldr);

      ctorIL.Emit(OpCodes.Ret);

      // 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).

      MethodBuilder myMthdBuilder = myTypeBuilder.DefineMethod("AddFundsFromPool",
                     MethodAttributes.Public,
                     typeof(double),
                     new Type[] { Type.GetType("System.Double&"),
							     typeof(double) });

      ParameterBuilder poolRefBuilder = myMthdBuilder.DefineParameter(1,
                     ParameterAttributes.Out,
                     "fundsPool");

      ParameterBuilder amountFromPoolBuilder = myMthdBuilder.DefineParameter(2,
                     ParameterAttributes.In,
                          "amountFromPool");

      ILGenerator mthdIL = myMthdBuilder.GetILGenerator();

      mthdIL.Emit(OpCodes.Ldarg_1);
      mthdIL.Emit(OpCodes.Ldarg_1);
      mthdIL.Emit(OpCodes.Ldind_R8);
      mthdIL.Emit(OpCodes.Ldarg_2);
      mthdIL.Emit(OpCodes.Sub);

      mthdIL.Emit(OpCodes.Stind_R8);

      mthdIL.Emit(OpCodes.Ldarg_0);
      mthdIL.Emit(OpCodes.Ldarg_0);
      mthdIL.Emit(OpCodes.Ldfld, balanceAmtBldr);
      mthdIL.Emit(OpCodes.Ldarg_2);
      mthdIL.Emit(OpCodes.Add);

      mthdIL.Emit(OpCodes.Stfld, balanceAmtBldr);

      mthdIL.Emit(OpCodes.Ldarg_0);
      mthdIL.Emit(OpCodes.Ldfld, balanceAmtBldr);
      mthdIL.Emit(OpCodes.Ret);

      return myTypeBuilder.CreateType();

   }

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      Type custType = null;
      object custObj = null;

      Type[] custArgTypes = new Type[] { typeof(string), typeof(string), typeof(double) };

      // Call the method to build our dynamic class.

      custType = BuildCustomerDataType();

      outputBlock.Text += "---" + "\n";

      ConstructorInfo myCustCtor = custType.GetConstructor(custArgTypes);
      double initialBalance = 100.00;
      custObj = myCustCtor.Invoke(new object[] { "Joe Consumer", 
						   "5678-XYZ", 
					  	   initialBalance });

      MemberInfo[] myMemberInfo = custType.GetMember("AddFundsFromPool");


      double thePool = 1000.00;
      outputBlock.Text += String.Format("The pool is currently ${0}", thePool) + "\n";
      outputBlock.Text += String.Format("The original balance of the account instance is ${0}",
                        initialBalance) + "\n";

      double amountFromPool = 50.00;
      outputBlock.Text += String.Format("The amount to be subtracted from the pool and added " +
              "to the account is ${0}", amountFromPool) + "\n";

      outputBlock.Text += "---" + "\n";
      outputBlock.Text += String.Format("Calling {0} ...", myMemberInfo[0].ToString()) + "\n";
      outputBlock.Text += "---" + "\n";

      object[] passMe = new object[] { thePool, amountFromPool };
      outputBlock.Text += String.Format("The new balance in the account instance is ${0}",
                  custType.InvokeMember("AddFundsFromPool",
                  BindingFlags.InvokeMethod,
                  null, custObj, passMe)) + "\n";
      thePool = (double)passMe[0];
      outputBlock.Text += String.Format("The new amount in the pool is ${0}", thePool) + "\n";

   }

}



Windows Phone OS

Supported in: 8.1, 8.0, 7.1

Windows Phone

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

Show:
© 2014 Microsoft