ParameterBuilder Class
Creates or associates parameter information.
Assembly: mscorlib (in mscorlib.dll)
| Name | Description | |
|---|---|---|
![]() | Attributes | Retrieves the attributes for this parameter. |
![]() | IsIn | Retrieves whether this is an input parameter. |
![]() | IsOptional | Retrieves whether this parameter is optional. |
![]() | IsOut | Retrieves whether this parameter is an output parameter. |
![]() | Name | Retrieves the name of this parameter. |
![]() | Position | Retrieves the signature position for this parameter. |
| Name | Description | |
|---|---|---|
![]() | Equals(Object^) | Determines whether the specified object is equal to the current object.(Inherited from Object.) |
![]() | Finalize() | Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.) |
![]() | GetHashCode() | Serves as the default hash function. (Inherited from Object.) |
![]() | GetToken() | Retrieves the token for this parameter. |
![]() | GetType() | |
![]() | MemberwiseClone() | |
![]() | SetConstant(Object^) | Sets the default value of the parameter. |
![]() | SetCustomAttribute(ConstructorInfo^, array<Byte>^) | Set a custom attribute using a specified custom attribute blob. |
![]() | SetCustomAttribute(CustomAttributeBuilder^) | Set a custom attribute using a custom attribute builder. |
![]() | SetMarshal(UnmanagedMarshal^) | Obsolete. Specifies the marshaling for this parameter. |
![]() | ToString() | Returns a string that represents the current object.(Inherited from Object.) |
| Name | Description | |
|---|---|---|
![]() ![]() | _ParameterBuilder::GetIDsOfNames(Guid%, IntPtr, UInt32, UInt32, IntPtr) | Maps a set of names to a corresponding set of dispatch identifiers. |
![]() ![]() | _ParameterBuilder::GetTypeInfo(UInt32, UInt32, IntPtr) | Retrieves the type information for an object, which can then be used to get the type information for an interface. |
![]() ![]() | _ParameterBuilder::GetTypeInfoCount(UInt32%) | Retrieves the number of type information interfaces that an object provides (either 0 or 1). |
![]() ![]() | _ParameterBuilder::Invoke(UInt32, Guid%, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) | Provides access to properties and methods exposed by an 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.
using namespace System; using namespace System::Threading; using namespace System::Reflection; using namespace System::Reflection::Emit; Type^ BuildCustomerDataType() { AppDomain^ myDomain = Thread::GetDomain(); AssemblyName^ myAsmName = gcnew 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", String::typeid, FieldAttributes::Private ); FieldBuilder^ acctIDBldr = myTypeBuilder->DefineField( "acctID", String::typeid, FieldAttributes::Private ); FieldBuilder^ balanceAmtBldr = myTypeBuilder->DefineField( "balanceAmt", double::typeid, FieldAttributes::Private ); array<Type^>^temp0 = {String::typeid,String::typeid,double::typeid}; ConstructorBuilder^ myCtorBuilder = myTypeBuilder->DefineConstructor( MethodAttributes::Public, CallingConventions::HasThis, temp0 ); ILGenerator^ ctorIL = myCtorBuilder->GetILGenerator(); Type^ objType = Type::GetType( "System.Object" ); ConstructorInfo^ objCtor = objType->GetConstructor( gcnew array<Type^>(0) ); 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). array<Type^>^temp4 = {Type::GetType( "System.Double&" ),double::typeid}; MethodBuilder^ myMthdBuilder = myTypeBuilder->DefineMethod( "AddFundsFromPool", MethodAttributes::Public, double::typeid, temp4 ); 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(); } int main() { Type^ custType = nullptr; Object^ custObj = nullptr; array<Type^>^custArgTypes = {String::typeid,String::typeid,double::typeid}; // Call the method to build our dynamic class. custType = BuildCustomerDataType(); Console::WriteLine( "---" ); ConstructorInfo^ myCustCtor = custType->GetConstructor( custArgTypes ); double initialBalance = 100.00; array<Object^>^temp5 = {"Joe Consumer","5678-XYZ",initialBalance}; custObj = myCustCtor->Invoke( temp5 ); array<MemberInfo^>^myMemberInfo = custType->GetMember( "AddFundsFromPool" ); double thePool = 1000.00; Console::WriteLine( "The pool is currently ${0}", thePool ); Console::WriteLine( "The original balance of the account instance is ${0}", initialBalance ); double amountFromPool = 50.00; Console::WriteLine( "The amount to be subtracted from the pool and added to the account is ${0}", amountFromPool ); Console::WriteLine( "---" ); Console::WriteLine( "Calling {0} ...", myMemberInfo[ 0 ] ); Console::WriteLine( "---" ); array<Object^>^passMe = {thePool,amountFromPool}; Console::WriteLine( "The new balance in the account instance is ${0}", custType->InvokeMember( "AddFundsFromPool", BindingFlags::InvokeMethod, nullptr, custObj, passMe ) ); thePool = safe_cast<double>(passMe[ 0 ]); Console::WriteLine( "The new amount in the pool is ${0}", thePool ); }
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.1
Any public static ( Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.




