本文整理汇总了C#中System.Reflection.Emit.MethodBuilder.DefineGenericParameters方法的典型用法代码示例。如果您正苦于以下问题:C# MethodBuilder.DefineGenericParameters方法的具体用法?C# MethodBuilder.DefineGenericParameters怎么用?C# MethodBuilder.DefineGenericParameters使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Reflection.Emit.MethodBuilder
的用法示例。
在下文中一共展示了MethodBuilder.DefineGenericParameters方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
using System;
using System.Reflection;
using System.Reflection.Emit;
class DemoMethodBuilder
{
public static void Main()
{
// Creating a dynamic assembly requires an AssemblyName
// object, and the current application domain.
//
AssemblyName asmName =
new AssemblyName("DemoMethodBuilder1");
AppDomain domain = AppDomain.CurrentDomain;
AssemblyBuilder demoAssembly =
domain.DefineDynamicAssembly(
asmName,
AssemblyBuilderAccess.RunAndSave
);
// Define the module that contains the code. For an
// assembly with one module, the module name is the
// assembly name plus a file extension.
ModuleBuilder demoModule =
demoAssembly.DefineDynamicModule(
asmName.Name,
asmName.Name + ".dll"
);
TypeBuilder demoType = demoModule.DefineType(
"DemoType",
TypeAttributes.Public | TypeAttributes.Abstract
);
// Define a Shared, Public method with standard calling
// conventions. Do not specify the parameter types or the
// return type, because type parameters will be used for
// those types, and the type parameters have not been
// defined yet.
MethodBuilder demoMethod = demoType.DefineMethod(
"DemoMethod",
MethodAttributes.Public | MethodAttributes.Static
);
// Defining generic parameters for the method makes it a
// generic method. By convention, type parameters are
// single alphabetic characters. T and U are used here.
//
string[] typeParamNames = {"T", "U"};
GenericTypeParameterBuilder[] typeParameters =
demoMethod.DefineGenericParameters(typeParamNames);
// The second type parameter is constrained to be a
// reference type.
typeParameters[1].SetGenericParameterAttributes(
GenericParameterAttributes.ReferenceTypeConstraint);
// Use the IsGenericMethod property to find out if a
// dynamic method is generic, and IsGenericMethodDefinition
// to find out if it defines a generic method.
Console.WriteLine("Is DemoMethod generic? {0}",
demoMethod.IsGenericMethod);
Console.WriteLine("Is DemoMethod a generic method definition? {0}",
demoMethod.IsGenericMethodDefinition);
// Set parameter types for the method. The method takes
// one parameter, and its type is specified by the first
// type parameter, T.
Type[] parms = {typeParameters[0]};
demoMethod.SetParameters(parms);
// Set the return type for the method. The return type is
// specified by the second type parameter, U.
demoMethod.SetReturnType(typeParameters[1]);
// Generate a code body for the method. The method doesn't
// do anything except return null.
//
ILGenerator ilgen = demoMethod.GetILGenerator();
ilgen.Emit(OpCodes.Ldnull);
ilgen.Emit(OpCodes.Ret);
// Complete the type.
Type dt = demoType.CreateType();
// To bind types to a dynamic generic method, you must
// first call the GetMethod method on the completed type.
// You can then define an array of types, and bind them
// to the method.
MethodInfo m = dt.GetMethod("DemoMethod");
Type[] typeArgs = {typeof(string), typeof(DemoMethodBuilder)};
MethodInfo bound = m.MakeGenericMethod(typeArgs);
// Display a string representing the bound method.
Console.WriteLine(bound);
// Save the assembly, so it can be examined with Ildasm.exe.
demoAssembly.Save(asmName.Name + ".dll");
}
}
输出:
Is DemoMethod generic? True Is DemoMethod a generic method definition? True DemoMethodBuilder DemoMethod[String,DemoMethodBuilder](System.String)