GenericTypeParameterBuilder Class
Defines and creates generic type parameters for dynamically defined generic types and methods. This class cannot be inherited.
Assembly: mscorlib (in mscorlib.dll)
System.Reflection::MemberInfo
System::Type
System.Reflection::TypeInfo
System.Reflection.Emit::GenericTypeParameterBuilder
| Name | Description | |
|---|---|---|
![]() | Assembly | Gets an Assembly object representing the dynamic assembly that contains the generic type definition the current type parameter belongs to.(Overrides Type::Assembly.) |
![]() | AssemblyQualifiedName | Gets null in all cases.(Overrides Type::AssemblyQualifiedName.) |
![]() | Attributes | |
![]() | BaseType | Gets the base type constraint of the current generic type parameter.(Overrides Type::BaseType.) |
![]() | ContainsGenericParameters | Gets true in all cases.(Overrides Type::ContainsGenericParameters.) |
![]() | CustomAttributes | Gets a collection that contains this member's custom attributes.(Inherited from MemberInfo.) |
![]() | DeclaredConstructors | Gets a collection of the constructors declared by the current type.(Inherited from TypeInfo.) |
![]() | DeclaredEvents | Gets a collection of the events defined by the current type.(Inherited from TypeInfo.) |
![]() | DeclaredFields | Gets a collection of the fields defined by the current type.(Inherited from TypeInfo.) |
![]() | DeclaredMembers | Gets a collection of the members defined by the current type.(Inherited from TypeInfo.) |
![]() | DeclaredMethods | Gets a collection of the methods defined by the current type.(Inherited from TypeInfo.) |
![]() | DeclaredNestedTypes | Gets a collection of the nested types defined by the current type.(Inherited from TypeInfo.) |
![]() | DeclaredProperties | Gets a collection of the properties defined by the current type. (Inherited from TypeInfo.) |
![]() | DeclaringMethod | Gets a MethodInfo that represents the declaring method, if the current GenericTypeParameterBuilder represents a type parameter of a generic method.(Overrides Type::DeclaringMethod.) |
![]() | DeclaringType | Gets the generic type definition or generic method definition to which the generic type parameter belongs.(Overrides Type::DeclaringType.) |
![]() | FullName | Gets null in all cases.(Overrides Type::FullName.) |
![]() | GenericParameterAttributes | Gets a combination of GenericParameterAttributes flags that describe the covariance and special constraints of the current generic type parameter. (Overrides Type::GenericParameterAttributes.) |
![]() | GenericParameterPosition | Gets the position of the type parameter in the type parameter list of the generic type or method that declared the parameter.(Overrides Type::GenericParameterPosition.) |
![]() | GenericTypeArguments | Gets an array of the generic type arguments for this type.(Inherited from Type.) |
![]() | GenericTypeParameters | Gets an array of the generic type parameters of the current instance. (Inherited from TypeInfo.) |
![]() | GUID | Not supported for incomplete generic type parameters.(Overrides Type::GUID.) |
![]() | HasElementType | |
![]() | ImplementedInterfaces | Gets a collection of the interfaces implemented by the current type.(Inherited from TypeInfo.) |
![]() | IsAbstract | |
![]() | IsAnsiClass | |
![]() | IsArray | Gets a value that indicates whether the type is an array.(Inherited from Type.) |
![]() | IsAutoClass | |
![]() | IsAutoLayout | Gets a value indicating whether the fields of the current type are laid out automatically by the common language runtime.(Inherited from Type.) |
![]() | IsByRef | |
![]() | IsClass | |
![]() | IsCOMObject | |
![]() | IsConstructedGenericType | Gets a value that indicates whether this object represents a constructed generic type.(Overrides Type::IsConstructedGenericType.) |
![]() | IsContextful | |
![]() | IsEnum | |
![]() | IsExplicitLayout | Gets a value indicating whether the fields of the current type are laid out at explicitly specified offsets.(Inherited from Type.) |
![]() | IsGenericParameter | Gets true in all cases.(Overrides Type::IsGenericParameter.) |
![]() | IsGenericType | Returns false in all cases.(Overrides Type::IsGenericType.) |
![]() | IsGenericTypeDefinition | Gets false in all cases.(Overrides Type::IsGenericTypeDefinition.) |
![]() | IsImport | Gets a value indicating whether the Type has a ComImportAttribute attribute applied, indicating that it was imported from a COM type library.(Inherited from Type.) |
![]() | IsInterface | |
![]() | IsLayoutSequential | Gets a value indicating whether the fields of the current type are laid out sequentially, in the order that they were defined or emitted to the metadata.(Inherited from Type.) |
![]() | IsMarshalByRef | |
![]() | IsNested | |
![]() | IsNestedAssembly | |
![]() | IsNestedFamANDAssem | |
![]() | IsNestedFamily | |
![]() | IsNestedFamORAssem | |
![]() | IsNestedPrivate | |
![]() | IsNestedPublic | Gets a value indicating whether a class is nested and declared public.(Inherited from Type.) |
![]() | IsNotPublic | |
![]() | IsPointer | |
![]() | IsPrimitive | |
![]() | IsPublic | |
![]() | IsSealed | |
![]() | IsSecurityCritical | Gets a value that indicates whether the current type is security-critical or security-safe-critical at the current trust level, and therefore can perform critical operations. (Inherited from Type.) |
![]() | IsSecuritySafeCritical | Gets a value that indicates whether the current type is security-safe-critical at the current trust level; that is, whether it can perform critical operations and can be accessed by transparent code. (Inherited from Type.) |
![]() | IsSecurityTransparent | Gets a value that indicates whether the current type is transparent at the current trust level, and therefore cannot perform critical operations.(Inherited from Type.) |
![]() | IsSerializable | |
![]() | IsSpecialName | Gets a value indicating whether the type has a name that requires special handling.(Inherited from Type.) |
![]() | IsUnicodeClass | |
![]() | IsValueType | |
![]() | IsVisible | |
![]() | MemberType | Gets a MemberTypes value indicating that this member is a type or a nested type.(Inherited from Type.) |
![]() | MetadataToken | Gets a value that identifies a metadata element.(Inherited from MemberInfo.) |
![]() | Module | Gets the dynamic module that contains the generic type parameter.(Overrides Type::Module.) |
![]() | Name | Gets the name of the generic type parameter.(Overrides MemberInfo::Name.) |
![]() | Namespace | Gets null in all cases.(Overrides Type::Namespace.) |
![]() | ReflectedType | Gets the Type object that was used to obtain the GenericTypeParameterBuilder.(Overrides Type::ReflectedType.) |
![]() | StructLayoutAttribute | Gets a StructLayoutAttribute that describes the layout of the current type.(Inherited from Type.) |
![]() | TypeHandle | Not supported for incomplete generic type parameters.(Overrides Type::TypeHandle.) |
![]() | TypeInitializer | Gets the initializer for the type.(Inherited from Type.) |
![]() | UnderlyingSystemType | Gets the current generic type parameter.(Overrides Type::UnderlyingSystemType.) |
| Name | Description | |
|---|---|---|
![]() | AsType() | |
![]() | Equals(Object^) | Tests whether the given object is an instance of EventToken and is equal to the current instance.(Overrides Type::Equals(Object^).) |
![]() | Equals(Type^) | |
![]() | FindInterfaces(TypeFilter^, Object^) | |
![]() | FindMembers(MemberTypes, BindingFlags, MemberFilter^, Object^) | Returns a filtered array of MemberInfo objects of the specified member type.(Inherited from Type.) |
![]() | GetArrayRank() | Gets the number of dimensions in an array. (Inherited from Type.) |
![]() | GetConstructor(BindingFlags, Binder^, CallingConventions, array<Type^>^, array<ParameterModifier>^) | Searches for a constructor whose parameters match the specified argument types and modifiers, using the specified binding constraints and the specified calling convention.(Inherited from Type.) |
![]() | GetConstructor(BindingFlags, Binder^, array<Type^>^, array<ParameterModifier>^) | Searches for a constructor whose parameters match the specified argument types and modifiers, using the specified binding constraints.(Inherited from Type.) |
![]() | GetConstructor(array<Type^>^) | Searches for a public instance constructor whose parameters match the types in the specified array.(Inherited from Type.) |
![]() | GetConstructors() | |
![]() | GetConstructors(BindingFlags) | Not supported for incomplete generic type parameters.(Overrides Type::GetConstructors(BindingFlags).) |
![]() | GetCustomAttributes(Boolean) | Not supported for incomplete generic type parameters.(Overrides MemberInfo::GetCustomAttributes(Boolean).) |
![]() | GetCustomAttributes(Type^, Boolean) | Not supported for incomplete generic type parameters.(Overrides MemberInfo::GetCustomAttributes(Type^, Boolean).) |
![]() | GetCustomAttributesData() | Returns a list of CustomAttributeData objects representing data about the attributes that have been applied to the target member.(Inherited from MemberInfo.) |
![]() | GetDeclaredEvent(String^) | Returns an object that represents the specified public event declared by the current type.(Inherited from TypeInfo.) |
![]() | GetDeclaredField(String^) | Returns an object that represents the specified public field declared by the current type.(Inherited from TypeInfo.) |
![]() | GetDeclaredMethod(String^) | Returns an object that represents the specified public method declared by the current type.(Inherited from TypeInfo.) |
![]() | GetDeclaredMethods(String^) | Returns a collection that contains all public methods declared on the current type that match the specified name.(Inherited from TypeInfo.) |
![]() | GetDeclaredNestedType(String^) | Returns an object that represents the specified public nested type declared by the current type.(Inherited from TypeInfo.) |
![]() | GetDeclaredProperty(String^) | Returns an object that represents the specified public property declared by the current type.(Inherited from TypeInfo.) |
![]() | GetDefaultMembers() | Searches for the members defined for the current Type whose DefaultMemberAttribute is set.(Inherited from Type.) |
![]() | GetElementType() | Throws a NotSupportedException in all cases. (Overrides Type::GetElementType().) |
![]() | GetEnumName(Object^) | Returns the name of the constant that has the specified value, for the current enumeration type.(Inherited from Type.) |
![]() | GetEnumNames() | Returns the names of the members of the current enumeration type.(Inherited from Type.) |
![]() | GetEnumUnderlyingType() | Returns the underlying type of the current enumeration type.(Inherited from Type.) |
![]() | GetEnumValues() | Returns an array of the values of the constants in the current enumeration type.(Inherited from Type.) |
![]() | GetEvent(String^) | |
![]() | GetEvent(String^, BindingFlags) | Not supported for incomplete generic type parameters.(Overrides Type::GetEvent(String^, BindingFlags).) |
![]() | GetEvents() | Not supported for incomplete generic type parameters.(Overrides Type::GetEvents().) |
![]() | GetEvents(BindingFlags) | Not supported for incomplete generic type parameters.(Overrides Type::GetEvents(BindingFlags).) |
![]() | GetField(String^) | Searches for the public field with the specified name.(Inherited from Type.) |
![]() | GetField(String^, BindingFlags) | Not supported for incomplete generic type parameters.(Overrides Type::GetField(String^, BindingFlags).) |
![]() | GetFields() | |
![]() | GetFields(BindingFlags) | Not supported for incomplete generic type parameters.(Overrides Type::GetFields(BindingFlags).) |
![]() | GetGenericArguments() | Not valid for generic type parameters.(Overrides Type::GetGenericArguments().) |
![]() | GetGenericParameterConstraints() | |
![]() | GetGenericTypeDefinition() | Not valid for generic type parameters.(Overrides Type::GetGenericTypeDefinition().) |
![]() | GetHashCode() | Returns a 32-bit integer hash code for the current instance.(Overrides Type::GetHashCode().) |
![]() | GetInterface(String^) | Searches for the interface with the specified name.(Inherited from Type.) |
![]() | GetInterface(String^, Boolean) | Not supported for incomplete generic type parameters.(Overrides Type::GetInterface(String^, Boolean).) |
![]() | GetInterfaceMap(Type^) | Not supported for incomplete generic type parameters.(Overrides Type::GetInterfaceMap(Type^).) |
![]() | GetInterfaces() | Not supported for incomplete generic type parameters.(Overrides Type::GetInterfaces().) |
![]() | GetMember(String^) | Searches for the public members with the specified name.(Inherited from Type.) |
![]() | GetMember(String^, BindingFlags) | Searches for the specified members, using the specified binding constraints.(Inherited from Type.) |
![]() | GetMember(String^, MemberTypes, BindingFlags) | Not supported for incomplete generic type parameters.(Overrides Type::GetMember(String^, MemberTypes, BindingFlags).) |
![]() | GetMembers() | |
![]() | GetMembers(BindingFlags) | Not supported for incomplete generic type parameters.(Overrides Type::GetMembers(BindingFlags).) |
![]() | GetMethod(String^) | Searches for the public method with the specified name.(Inherited from Type.) |
![]() | GetMethod(String^, BindingFlags) | Searches for the specified method, using the specified binding constraints.(Inherited from Type.) |
![]() | GetMethod(String^, BindingFlags, Binder^, CallingConventions, array<Type^>^, array<ParameterModifier>^) | Searches for the specified method whose parameters match the specified argument types and modifiers, using the specified binding constraints and the specified calling convention.(Inherited from Type.) |
![]() | GetMethod(String^, BindingFlags, Binder^, array<Type^>^, array<ParameterModifier>^) | Searches for the specified method whose parameters match the specified argument types and modifiers, using the specified binding constraints.(Inherited from Type.) |
![]() | GetMethod(String^, array<Type^>^) | Searches for the specified public method whose parameters match the specified argument types.(Inherited from Type.) |
![]() | GetMethod(String^, array<Type^>^, array<ParameterModifier>^) | Searches for the specified public method whose parameters match the specified argument types and modifiers.(Inherited from Type.) |
![]() | GetMethods() | |
![]() | GetMethods(BindingFlags) | Not supported for incomplete generic type parameters.(Overrides Type::GetMethods(BindingFlags).) |
![]() | GetNestedType(String^) | Searches for the public nested type with the specified name.(Inherited from Type.) |
![]() | GetNestedType(String^, BindingFlags) | Not supported for incomplete generic type parameters.(Overrides Type::GetNestedType(String^, BindingFlags).) |
![]() | GetNestedTypes() | |
![]() | GetNestedTypes(BindingFlags) | Not supported for incomplete generic type parameters.(Overrides Type::GetNestedTypes(BindingFlags).) |
![]() | GetProperties() | |
![]() | GetProperties(BindingFlags) | Not supported for incomplete generic type parameters.(Overrides Type::GetProperties(BindingFlags).) |
![]() | GetProperty(String^) | Searches for the public property with the specified name.(Inherited from Type.) |
![]() | GetProperty(String^, BindingFlags) | Searches for the specified property, using the specified binding constraints.(Inherited from Type.) |
![]() | GetProperty(String^, BindingFlags, Binder^, Type^, array<Type^>^, array<ParameterModifier>^) | Searches for the specified property whose parameters match the specified argument types and modifiers, using the specified binding constraints.(Inherited from Type.) |
![]() | GetProperty(String^, Type^) | Searches for the public property with the specified name and return type.(Inherited from Type.) |
![]() | GetProperty(String^, Type^, array<Type^>^) | Searches for the specified public property whose parameters match the specified argument types.(Inherited from Type.) |
![]() | GetProperty(String^, Type^, array<Type^>^, array<ParameterModifier>^) | Searches for the specified public property whose parameters match the specified argument types and modifiers.(Inherited from Type.) |
![]() | GetProperty(String^, array<Type^>^) | Searches for the specified public property whose parameters match the specified argument types.(Inherited from Type.) |
![]() | GetType() | |
![]() | InvokeMember(String^, BindingFlags, Binder^, Object^, array<Object^>^) | Invokes the specified member, using the specified binding constraints and matching the specified argument list.(Inherited from Type.) |
![]() | InvokeMember(String^, BindingFlags, Binder^, Object^, array<Object^>^, CultureInfo^) | Invokes the specified member, using the specified binding constraints and matching the specified argument list and culture.(Inherited from Type.) |
![]() | InvokeMember(String^, BindingFlags, Binder^, Object^, array<Object^>^, array<ParameterModifier>^, CultureInfo^, array<String^>^) | Not supported for incomplete generic type parameters.(Overrides Type::InvokeMember(String^, BindingFlags, Binder^, Object^, array<Object^>^, array<ParameterModifier>^, CultureInfo^, array<String^>^).) |
![]() | IsAssignableFrom(Type^) | Throws a NotSupportedException exception in all cases.(Overrides Type::IsAssignableFrom(Type^).) |
![]() | IsAssignableFrom(TypeInfo^) | Throws a NotSupportedException exception in all cases.(Overrides TypeInfo::IsAssignableFrom(TypeInfo^).) |
![]() | IsDefined(Type^, Boolean) | Not supported for incomplete generic type parameters.(Overrides MemberInfo::IsDefined(Type^, Boolean).) |
![]() | IsEnumDefined(Object^) | Returns a value that indicates whether the specified value exists in the current enumeration type.(Inherited from Type.) |
![]() | IsEquivalentTo(Type^) | Determines whether two COM types have the same identity and are eligible for type equivalence.(Inherited from Type.) |
![]() | IsInstanceOfType(Object^) | |
![]() | IsSubclassOf(Type^) | Not supported for incomplete generic type parameters.(Overrides Type::IsSubclassOf(Type^).) |
![]() | MakeArrayType() | Returns the type of a one-dimensional array whose element type is the generic type parameter.(Overrides Type::MakeArrayType().) |
![]() | MakeArrayType(Int32) | Returns the type of an array whose element type is the generic type parameter, with the specified number of dimensions.(Overrides Type::MakeArrayType(Int32).) |
![]() | MakeByRefType() | Returns a Type object that represents the current generic type parameter when passed as a reference parameter.(Overrides Type::MakeByRefType().) |
![]() | MakeGenericType(array<Type^>^) | Not valid for incomplete generic type parameters.(Overrides Type::MakeGenericType(array<Type^>^).) |
![]() | MakePointerType() | Returns a Type object that represents a pointer to the current generic type parameter.(Overrides Type::MakePointerType().) |
![]() | SetBaseTypeConstraint(Type^) | Sets the base type that a type must inherit in order to be substituted for the type parameter. |
![]() | SetCustomAttribute(ConstructorInfo^, array<Byte>^) | Sets a custom attribute using a specified custom attribute blob. |
![]() | SetCustomAttribute(CustomAttributeBuilder^) | Set a custom attribute using a custom attribute builder. |
![]() | SetGenericParameterAttributes(GenericParameterAttributes) | Sets the variance characteristics and special constraints of the generic parameter, such as the parameterless constructor constraint. |
![]() | SetInterfaceConstraints(array<Type^>^) | Sets the interfaces a type must implement in order to be substituted for the type parameter. |
![]() | ToString() | Returns a string representation of the current generic type parameter.(Overrides Type::ToString().) |
| Name | Description | |
|---|---|---|
![]() ![]() | IReflectableType::GetTypeInfo() | |
![]() ![]() | _MemberInfo::GetIDsOfNames(Guid%, IntPtr, UInt32, UInt32, IntPtr) | Maps a set of names to a corresponding set of dispatch identifiers.(Inherited from MemberInfo.) |
![]() ![]() | _MemberInfo::GetType() | Gets a Type object representing the MemberInfo class.(Inherited from MemberInfo.) |
![]() ![]() | _MemberInfo::GetTypeInfo(UInt32, UInt32, IntPtr) | Retrieves the type information for an object, which can then be used to get the type information for an interface.(Inherited from MemberInfo.) |
![]() ![]() | _MemberInfo::GetTypeInfoCount(UInt32%) | Retrieves the number of type information interfaces that an object provides (either 0 or 1).(Inherited from MemberInfo.) |
![]() ![]() | _MemberInfo::Invoke(UInt32, Guid%, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) | Provides access to properties and methods exposed by an object.(Inherited from MemberInfo.) |
![]() ![]() | _Type::GetIDsOfNames(Guid%, IntPtr, UInt32, UInt32, IntPtr) | Maps a set of names to a corresponding set of dispatch identifiers.(Inherited from Type.) |
![]() ![]() | _Type::GetTypeInfo(UInt32, UInt32, IntPtr) | Retrieves the type information for an object, which can then be used to get the type information for an interface.(Inherited from Type.) |
![]() ![]() | _Type::GetTypeInfoCount(UInt32%) | Retrieves the number of type information interfaces that an object provides (either 0 or 1).(Inherited from Type.) |
![]() ![]() | _Type::Invoke(UInt32, Guid%, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) | Provides access to properties and methods exposed by an object.(Inherited from Type.) |
| Name | Description | |
|---|---|---|
![]() | GetCustomAttribute(Type^) | Overloaded. Retrieves a custom attribute of a specified type that is applied to a specified member.(Defined by CustomAttributeExtensions.) |
![]() | GetCustomAttribute(Type^, Boolean) | Overloaded. Retrieves a custom attribute of a specified type that is applied to a specified member, and optionally inspects the ancestors of that member.(Defined by CustomAttributeExtensions.) |
![]() | GetCustomAttribute<T>() | Overloaded. Retrieves a custom attribute of a specified type that is applied to a specified member.(Defined by CustomAttributeExtensions.) |
![]() | GetCustomAttribute<T>(Boolean) | Overloaded. Retrieves a custom attribute of a specified type that is applied to a specified member, and optionally inspects the ancestors of that member.(Defined by CustomAttributeExtensions.) |
![]() | GetCustomAttributes() | Overloaded. Retrieves a collection of custom attributes that are applied to a specified member.(Defined by CustomAttributeExtensions.) |
![]() | GetCustomAttributes(Type^) | Overloaded. Retrieves a collection of custom attributes of a specified type that are applied to a specified member.(Defined by CustomAttributeExtensions.) |
![]() | GetCustomAttributes<T>(Boolean) | Overloaded. Retrieves a collection of custom attributes of a specified type that are applied to a specified member, and optionally inspects the ancestors of that member.(Defined by CustomAttributeExtensions.) |
![]() | GetRuntimeEvent(String^) | Retrieves an object that represents the specified event.(Defined by RuntimeReflectionExtensions.) |
![]() | GetRuntimeEvents() | Retrieves a collection that represents all the events defined on a specified type.(Defined by RuntimeReflectionExtensions.) |
![]() | GetRuntimeField(String^) | Retrieves an object that represents a specified field.(Defined by RuntimeReflectionExtensions.) |
![]() | GetRuntimeFields() | Retrieves a collection that represents all the fields defined on a specified type.(Defined by RuntimeReflectionExtensions.) |
![]() | GetRuntimeInterfaceMap(Type^) | Returns an interface mapping for the specified type and the specified interface.(Defined by RuntimeReflectionExtensions.) |
![]() | GetRuntimeMethod(String^, array<Type^>^) | Retrieves an object that represents a specified method.(Defined by RuntimeReflectionExtensions.) |
![]() | GetRuntimeMethods() | Retrieves a collection that represents all methods defined on a specified type.(Defined by RuntimeReflectionExtensions.) |
![]() | GetRuntimeProperties() | Retrieves a collection that represents all the properties defined on a specified type.(Defined by RuntimeReflectionExtensions.) |
![]() | GetRuntimeProperty(String^) | Retrieves an object that represents a specified property.(Defined by RuntimeReflectionExtensions.) |
![]() | GetTypeInfo() | Returns the TypeInfo representation of the specified type.(Defined by IntrospectionExtensions.) |
![]() | IsDefined(Type^) | Overloaded. Indicates whether custom attributes of a specified type are applied to a specified member.(Defined by CustomAttributeExtensions.) |
You can get an array of GenericTypeParameterBuilder objects by using the TypeBuilder::DefineGenericParameters method to add type parameters to a dynamic type, thus making it a generic type, or by using the MethodBuilder::DefineGenericParameters method to add type parameters to a dynamic method. Use the GenericTypeParameterBuilder objects to add constraints to the type parameters. Constraints are of three kinds:
The base type constraint specifies that any type assigned to the generic type parameter must derive from a particular base type. Set this constraint by using the SetBaseTypeConstraint method.
An interface constraint specifies that any type assigned to the generic type parameter must implement a particular interface. Set the interface constraints by using the SetInterfaceConstraints method.
Special constraints specify that any type assigned to the generic type parameter must have a default constructor, must be a reference type, or must be a value type. Set the special constraints for a type parameter by using the SetGenericParameterAttributes method.
Interface constraints and special constraints cannot be retrieved using methods of the GenericTypeParameterBuilder class. Once you have created the generic type that contains the type parameters, you can use its Type object to reflect the constraints. Use the Type::GetGenericArguments method to get the type parameters, and for each type parameter use the Type::GetGenericParameterConstraints method to get the base type constraint and interface constraints, and the Type::GenericParameterAttributes property to get the special constraints.
The following code example creates a generic type with two type parameters, and saves them in the assembly GenericEmitExample1.dll. You can use the Ildasm.exe (IL Disassembler) to view the generated types. For a more detailed explanation of the steps involved in defining a dynamic generic type, see How to: Define a Generic Type with Reflection Emit.
using namespace System; using namespace System::Reflection; using namespace System::Reflection::Emit; using namespace System::Collections::Generic; // Dummy class to satisfy TFirst constraints. // public ref class Example {}; // Define a trivial base class and two trivial interfaces // to use when demonstrating constraints. // public ref class ExampleBase {}; public interface class IExampleA {}; public interface class IExampleB {}; // Define a trivial type that can substitute for type parameter // TSecond. // public ref class ExampleDerived : ExampleBase, IExampleA, IExampleB {}; // List the constraint flags. The GenericParameterAttributes // enumeration contains two sets of attributes, variance and // constraints. For this example, only constraints are used. // static void ListConstraintAttributes( Type^ t ) { // Mask off the constraint flags. GenericParameterAttributes constraints = t->GenericParameterAttributes & GenericParameterAttributes::SpecialConstraintMask; if ((constraints & GenericParameterAttributes::ReferenceTypeConstraint) != GenericParameterAttributes::None) Console::WriteLine( L" ReferenceTypeConstraint"); if ((constraints & GenericParameterAttributes::NotNullableValueTypeConstraint) != GenericParameterAttributes::None) Console::WriteLine( L" NotNullableValueTypeConstraint"); if ((constraints & GenericParameterAttributes::DefaultConstructorConstraint) != GenericParameterAttributes::None) Console::WriteLine( L" DefaultConstructorConstraint"); } static void DisplayGenericParameters( Type^ t ) { if (!t->IsGenericType) { Console::WriteLine( L"Type '{0}' is not generic." ); return; } if (!t->IsGenericTypeDefinition) t = t->GetGenericTypeDefinition(); array<Type^>^ typeParameters = t->GetGenericArguments(); Console::WriteLine( L"\r\nListing {0} type parameters for type '{1}'.", typeParameters->Length, t ); for each ( Type^ tParam in typeParameters ) { Console::WriteLine( L"\r\nType parameter {0}:", tParam->ToString() ); for each (Type^ c in tParam->GetGenericParameterConstraints()) { if (c->IsInterface) Console::WriteLine( L" Interface constraint: {0}", c); else Console::WriteLine( L" Base type constraint: {0}", c); } ListConstraintAttributes(tParam); } } void main() { // Define a dynamic assembly to contain the sample type. The // assembly will be run and also saved to disk, so // AssemblyBuilderAccess.RunAndSave is specified. // AppDomain^ myDomain = AppDomain::CurrentDomain; AssemblyName^ myAsmName = gcnew AssemblyName( L"GenericEmitExample1" ); AssemblyBuilder^ myAssembly = myDomain->DefineDynamicAssembly( myAsmName, AssemblyBuilderAccess::RunAndSave ); // An assembly is made up of executable modules. For a single- // module assembly, the module name and file name are the same // as the assembly name. // ModuleBuilder^ myModule = myAssembly->DefineDynamicModule( myAsmName->Name, String::Concat( myAsmName->Name, L".dll" ) ); // Get type objects for the base class trivial interfaces to // be used as constraints. // Type^ baseType = ExampleBase::typeid; Type^ interfaceA = IExampleA::typeid; Type^ interfaceB = IExampleB::typeid; // Define the sample type. // TypeBuilder^ myType = myModule->DefineType( L"Sample", TypeAttributes::Public ); Console::WriteLine( L"Type 'Sample' is generic: {0}", myType->IsGenericType ); // Define type parameters for the type. Until you do this, // the type is not generic, as the preceding and following // WriteLine statements show. The type parameter names are // specified as an array of strings. To make the code // easier to read, each GenericTypeParameterBuilder is placed // in a variable with the same name as the type parameter. // array<String^>^typeParamNames = {L"TFirst",L"TSecond"}; array<GenericTypeParameterBuilder^>^typeParams = myType->DefineGenericParameters( typeParamNames ); GenericTypeParameterBuilder^ TFirst = typeParams[0]; GenericTypeParameterBuilder^ TSecond = typeParams[1]; Console::WriteLine( L"Type 'Sample' is generic: {0}", myType->IsGenericType ); // Apply constraints to the type parameters. // // A type that is substituted for the first parameter, TFirst, // must be a reference type and must have a parameterless // constructor. TFirst->SetGenericParameterAttributes( GenericParameterAttributes::DefaultConstructorConstraint | GenericParameterAttributes::ReferenceTypeConstraint ); // A type that is substituted for the second type // parameter must implement IExampleA and IExampleB, and // inherit from the trivial test class ExampleBase. The // interface constraints are specified as an array // containing the interface types. array<Type^>^interfaceTypes = { interfaceA, interfaceB }; TSecond->SetInterfaceConstraints( interfaceTypes ); TSecond->SetBaseTypeConstraint( baseType ); // The following code adds a private field named ExampleField, // of type TFirst. FieldBuilder^ exField = myType->DefineField("ExampleField", TFirst, FieldAttributes::Private); // Define a static method that takes an array of TFirst and // returns a List<TFirst> containing all the elements of // the array. To define this method it is necessary to create // the type List<TFirst> by calling MakeGenericType on the // generic type definition, generic<T> List. // The parameter type is created by using the // MakeArrayType method. // Type^ listOf = List::typeid; Type^ listOfTFirst = listOf->MakeGenericType(TFirst); array<Type^>^ mParamTypes = { TFirst->MakeArrayType() }; MethodBuilder^ exMethod = myType->DefineMethod("ExampleMethod", MethodAttributes::Public | MethodAttributes::Static, listOfTFirst, mParamTypes); // Emit the method body. // The method body consists of just three opcodes, to load // the input array onto the execution stack, to call the // List<TFirst> constructor that takes IEnumerable<TFirst>, // which does all the work of putting the input elements into // the list, and to return, leaving the list on the stack. The // hard work is getting the constructor. // // The GetConstructor method is not supported on a // GenericTypeParameterBuilder, so it is not possible to get // the constructor of List<TFirst> directly. There are two // steps, first getting the constructor of generic<T> List and then // calling a method that converts it to the corresponding // constructor of List<TFirst>. // // The constructor needed here is the one that takes an // IEnumerable<T>. Note, however, that this is not the // generic type definition of generic<T> IEnumerable; instead, the // T from generic<T> List must be substituted for the T of // generic<T> IEnumerable. (This seems confusing only because both // types have type parameters named T. That is why this example // uses the somewhat silly names TFirst and TSecond.) To get // the type of the constructor argument, take the generic // type definition generic<T> IEnumerable and // call MakeGenericType with the first generic type parameter // of generic<T> List. The constructor argument list must be passed // as an array, with just one argument in this case. // // Now it is possible to get the constructor of generic<T> List, // using GetConstructor on the generic type definition. To get // the constructor of List<TFirst>, pass List<TFirst> and // the constructor from generic<T> List to the static // TypeBuilder.GetConstructor method. // ILGenerator^ ilgen = exMethod->GetILGenerator(); Type^ ienumOf = IEnumerable::typeid; Type^ TfromListOf = listOf->GetGenericArguments()[0]; Type^ ienumOfT = ienumOf->MakeGenericType(TfromListOf); array<Type^>^ ctorArgs = {ienumOfT}; ConstructorInfo^ ctorPrep = listOf->GetConstructor(ctorArgs); ConstructorInfo^ ctor = TypeBuilder::GetConstructor(listOfTFirst, ctorPrep); ilgen->Emit(OpCodes::Ldarg_0); ilgen->Emit(OpCodes::Newobj, ctor); ilgen->Emit(OpCodes::Ret); // Create the type and save the assembly. Type^ finished = myType->CreateType(); myAssembly->Save( String::Concat( myAsmName->Name, L".dll" ) ); // Invoke the method. // ExampleMethod is not generic, but the type it belongs to is // generic, so in order to get a MethodInfo that can be invoked // it is necessary to create a constructed type. The Example // class satisfies the constraints on TFirst, because it is a // reference type and has a default constructor. In order to // have a class that satisfies the constraints on TSecond, // this code example defines the ExampleDerived type. These // two types are passed to MakeGenericMethod to create the // constructed type. // array<Type^>^ typeArgs = { Example::typeid, ExampleDerived::typeid }; Type^ constructed = finished->MakeGenericType(typeArgs); MethodInfo^ mi = constructed->GetMethod("ExampleMethod"); // Create an array of Example objects, as input to the generic // method. This array must be passed as the only element of an // array of arguments. The first argument of Invoke is // null, because ExampleMethod is static. Display the count // on the resulting List<Example>. // array<Example^>^ input = { gcnew Example(), gcnew Example() }; array<Object^>^ arguments = { input }; List<Example^>^ listX = (List<Example^>^) mi->Invoke(nullptr, arguments); Console::WriteLine( "\nThere are {0} elements in the List<Example>.", listX->Count); DisplayGenericParameters(finished); } /* This code example produces the following output: Type 'Sample' is generic: False Type 'Sample' is generic: True There are 2 elements in the List<Example>. Listing 2 type parameters for type 'Sample[TFirst,TSecond]'. Type parameter TFirst: ReferenceTypeConstraint DefaultConstructorConstraint Type parameter TSecond: Interface constraint: IExampleA Interface constraint: IExampleB Base type constraint: ExampleBase */
Available since 2.0
Silverlight
Available since 2.0
Any public static ( Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.



