gpt4 book ai didi

C# 反射 : How do I initialize a field created dynamically in TypeBuilder?

转载 作者:太空宇宙 更新时间:2023-11-03 20:30:36 24 4
gpt4 key购买 nike

我正在创建一个动态 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");

最佳答案

从我的 answer 复制意大利面对于您的其他(非常相似)问题,但是:

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");

// Store a handle to the ReferenceObject(int32 pValue)
// constructor.
ConstructorInfo referenceObjectConstructor = typeof(ReferenceObject).GetConstructor(new[] { typeof(int) });

foreach (ReferenceType rt in GetTypes())
{
TypeBuilder tb = mb.DefineType(rt.Name, TypeAttributes.Public);

// Define a static constructor to populate the ReferenceObject
// fields.
ConstructorBuilder staticConstructorBuilder = tb.DefineConstructor(MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, Type.EmptyTypes);
ILGenerator staticConstructorILGenerator = staticConstructorBuilder.GetILGenerator();

foreach (Reference r in GetReferences(rt.ID))
{
string name = r.Abbreviation.Trim();

// Create a public, static, readonly field to store the
// named ReferenceObject.
FieldBuilder referenceObjectField = tb.DefineField(name, typeof(ReferenceObject), FieldAttributes.Static | FieldAttributes.Public | FieldAttributes.InitOnly);

// Add code to the static constructor to populate the
// ReferenceObject field:

// Load the ReferenceObject's ID value onto the stack as a
// literal 4-byte integer (Int32).
staticConstructorILGenerator.Emit(OpCodes.Ldc_I4, r.ID);

// Create a reference to a new ReferenceObject on the stack
// by calling the ReferenceObject(int32 pValue) reference
// we created earlier.
staticConstructorILGenerator.Emit(OpCodes.Newobj, referenceObjectConstructor);

// Store the ReferenceObject reference to the static
// ReferenceObject field.
staticConstructorILGenerator.Emit(OpCodes.Stsfld, referenceObjectField);
}

// Finish the static constructor.
staticConstructorILGenerator.Emit(OpCodes.Ret);

tb.CreateType();
}

ab.Save(aName.Name + ".dll");

关于C# 反射 : How do I initialize a field created dynamically in TypeBuilder?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7766964/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com