c# - 在 .net c# 代码中创建类类型

标签 c# .net linq class expression

当我寻找 System.Linq.Expression 功能(在代码中创建表达式树、编译它们、执行它们)时,我问自己这是否也可以用于类创建 - 特别是泛型类。

我期望类似 Expression.Class() 或 Expression.GenericClass() 的东西。查看这些方法,我没有看到任何这些方法。这将非常实用,因为我可以动态地构建业务对象——即时的。其实这就是我所需要的。如果 .net c# 中有另一种方法,我也感兴趣。

最佳答案

All-In-One Framework codeplex 项目中有一个示例说明如何执行此操作 http://1code.codeplex.com/

// Define the assembly and the module.

AppDomain appDomain = AppDomain.CurrentDomain;
AssemblyName assemblyName = new AssemblyName("EmittedAssembly");
AssemblyBuilder assembly = appDomain.DefineDynamicAssembly(
assemblyName, 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 module = assembly.DefineDynamicModule(
assemblyName.Name, assemblyName.Name + ".dll");


/////////////////////////////////////////////////////////////////////
// Declare the types (classes).
// 

// Declare the class "ClassA"
TypeBuilder classA = module.DefineType("ClassA", TypeAttributes.Public);
// Declare the class "ClassB"
TypeBuilder classB = module.DefineType("ClassB", TypeAttributes.Public);

// Define the fields stringField, classBField
FieldBuilder stringField = classA.DefineField("stringField",
typeof(string), FieldAttributes.Private);
FieldBuilder classBField = classA.DefineField("classBField",
classB, FieldAttributes.Public);

/////////////////////////////////////////////////////////////////////
// Define the property ClassBProperty
PropertyBuilder classBProperty = classA.DefineProperty(
    "ClassBProperty", PropertyAttributes.None, classB, null);

// The special set of attributes for the property set&get methods
MethodAttributes getSetAttr = MethodAttributes.Public |
    MethodAttributes.SpecialName | MethodAttributes.HideBySig;

// Define the "get" accessor method for ClassBProperty
MethodBuilder classBGetProp = classA.DefineMethod(
    "get_ClassBProperty", getSetAttr, classB, Type.EmptyTypes);
ILGenerator classBGetIL = classBGetProp.GetILGenerator();
classBGetIL.Emit(OpCodes.Ldarg_0);
classBGetIL.Emit(OpCodes.Ldfld, classBField);
classBGetIL.Emit(OpCodes.Ret);

// Define the "set" accessor method for ClassBProperty
MethodBuilder classBSetProp = classA.DefineMethod(
    "set_ClassBProperty", getSetAttr, null, new Type[] { classB });
ILGenerator sampleSetIL = classBSetProp.GetILGenerator();
sampleSetIL.Emit(OpCodes.Ldarg_0);
sampleSetIL.Emit(OpCodes.Ldarg_1);
sampleSetIL.Emit(OpCodes.Stfld, classBField);
sampleSetIL.Emit(OpCodes.Ret);

// Map the get&set methods to PropertyBuilder
classBProperty.SetGetMethod(classBGetProp);
classBProperty.SetSetMethod(classBSetProp);

/////////////////////////////////////////////////////////////////////
// Define a method that uses the classBField
MethodBuilder classAMethod = classA.DefineMethod("ClassAMethod", 
    MethodAttributes.Public);

// Define the list generics and ienumerable generic
Type listOf = typeof(List<>);
Type enumOf = typeof(IEnumerable<>);
Type listOfClassA = listOf.MakeGenericType(classA);
Type enumOfClassA = enumOf.MakeGenericType(classA);

// Define the method, ClassBMethod, for ClassB
MethodBuilder classBMethod = classB.DefineMethod("ClassBMethod", 
    MethodAttributes.Public, typeof(void), new Type[] { listOfClassA });
classBMethod.DefineParameter(1, ParameterAttributes.None, "list");

// Write the body of ClassAMethod that calls ClassBMethod
ILGenerator ilgenA = classAMethod.GetILGenerator();
ilgenA.Emit(OpCodes.Nop);
ilgenA.Emit(OpCodes.Ldarg_0);
ilgenA.Emit(OpCodes.Ldfld, classBField);
ilgenA.Emit(OpCodes.Ldnull);
ilgenA.Emit(OpCodes.Callvirt, classBMethod);
ilgenA.Emit(OpCodes.Ret);

/////////////////////////////////////////////////////////////////////
// Create the types.
// 

classA.CreateType();
classB.CreateType();    

/////////////////////////////////////////////////////////////////////
// Save the assembly.
// 

assembly.Save(assemblyName.Name + ".dll");

抱歉,这有点长..:-P

关于c# - 在 .net c# 代码中创建类类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3378033/

相关文章:

c# - 无法在asp.net core mvc中调用具有带有[FromBody]属性的类参数的API Post方法

c# - 更改dataGridView中垂直滚动条的宽度

.net - 如何在 .NET 中使用 TreeView 控件创建三态复选框?

c# - 使用匿名类型列表填充 AutoCompleteBox

c# - MS Graph - LINQ 查询返回不正确的结果

c# - ASP.NET MVC 静态存储库?

c# - Windows Phone 7 从应用程序访问 Dropbox

c# linq 连接具有不同名称的多个属性

.net - Win32异常 :The target principal name is incorrect when calling a WCF service with WebClient

c# - Libgit2sharp 迭代提交从最新到最旧