C# 反思:如何初始化在 TypeBuilder 中动态创建的字段?
我正在创建一个动态 dll 来保存从我的数据库创建的自定义对象。我可以按照我想要的方式创建字段,但是我不明白如何调用构造函数。对于最终生成的结果,我想要:
public class Countries
{
public Countries() { }
public static readonly ReferenceObject USA = new ReferenceObject(120);
public static readonly ReferenceObject CAN = new ReferenceObject(13);
public static readonly ReferenceObject MEX = new ReferenceObject(65);
... //These would be populated from the database
}
我得到的是
public class Countries
{
public Countries() { }
public static readonly ReferenceObject USA;
public static readonly ReferenceObject CAN;
public static readonly ReferenceObject MEX;
...
}
如何将值设置为新的初始化对象?
AppDomain domain = AppDomain.CurrentDomain;
AssemblyName aName = new AssemblyName("DynamicEnums");
AssemblyBuilder ab = domain.DefineDynamicAssembly(aName, AssemblyBuilderAccess.Save);
ModuleBuilder mb = ab.DefineDynamicModule(aName.Name, aName.Name + ".dll");
foreach(ReferenceType rt in GetTypes())
{
TypeBuilder tb = mb.DefineType(rt.Name, TypeAttributes.Public);
foreach (Reference r in GetReferences(rt.ID))
{
string name = NameFix(r.Name);
FieldBuilder fb = tb.DefineField(name, typeof(ReferenceObject), FieldAttributes.Static | FieldAttributes.Public | FieldAttributes.Literal);
//Call constructor here... how???
}
types.Add(tb.CreateType());
}
ab.Save(aName.Name + ".dll");
I'm creating a dynamic dll to hold custom objects created from my database. I can create the field the way I want, however I don't understand how to call the constructor. For a final generated result, I want:
public class Countries
{
public Countries() { }
public static readonly ReferenceObject USA = new ReferenceObject(120);
public static readonly ReferenceObject CAN = new ReferenceObject(13);
public static readonly ReferenceObject MEX = new ReferenceObject(65);
... //These would be populated from the database
}
what I'm getting is
public class Countries
{
public Countries() { }
public static readonly ReferenceObject USA;
public static readonly ReferenceObject CAN;
public static readonly ReferenceObject MEX;
...
}
How do I set the values to new initialized objects?
AppDomain domain = AppDomain.CurrentDomain;
AssemblyName aName = new AssemblyName("DynamicEnums");
AssemblyBuilder ab = domain.DefineDynamicAssembly(aName, AssemblyBuilderAccess.Save);
ModuleBuilder mb = ab.DefineDynamicModule(aName.Name, aName.Name + ".dll");
foreach(ReferenceType rt in GetTypes())
{
TypeBuilder tb = mb.DefineType(rt.Name, TypeAttributes.Public);
foreach (Reference r in GetReferences(rt.ID))
{
string name = NameFix(r.Name);
FieldBuilder fb = tb.DefineField(name, typeof(ReferenceObject), FieldAttributes.Static | FieldAttributes.Public | FieldAttributes.Literal);
//Call constructor here... how???
}
types.Add(tb.CreateType());
}
ab.Save(aName.Name + ".dll");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
来自我的 回答您的其他(非常相似)问题,但是:
Bit of a copy-pasta from my answer to your other (very similar) question, but:
当编写一个 C# 类(其中的字段像您的示例中那样进行初始化)时,编译器所做的就是在构造函数中生成初始化代码。
这就是我认为您应该做的:使用 TypeBuilder.DefineConstructor 生成构造函数,并在内部创建代码来设置字段。
When one writes a C# class where fields are initialized like in your example, what the compiler does is generate initialization code in the constructor.
This is what I think you should do: use
TypeBuilder.DefineConstructor
to generate a constructor and, inside, create code to set the fields.