i need help with .net reflection.emit
Need create simple Assembly with public struct and string field in it. Field must be constant and i also need define it. In all i need get Assembly that hold inside something like this:
namespace n {
struct Alpha {
public const string DATA = "Alpha";
}
}
I don't understand how create string field and how define it.
At this moment i am write this code:
private static void Generate() {
var an = new AssemblyName("Beta") { Version = new Version("1.0.0.0") };
var ab = AppDomain.CurrentDomain.DefineDynamicAssembly(an, AssemblyBuilderAccess.Save); // ToDo добавить путь к дирректории bin/debug проекта etalon
var mb = ab.DefineDynamicModule("BetaModule", "Beta.dll");
var tb = mb.DefineType("n.Beta", TypeAttributes.Public, typeof(System.ValueType));
// what i need do after it? how i understand from MSDN i need call DefineInitializedData method but i am not shure how do it.
tb.CreateType();
ab.Save("Beta.dll");
}
Solution:
private static void Generate() {
var an = new AssemblyName("Beta") { Version = new Version("1.0.0.0") };
var ab = AppDomain.CurrentDomain.DefineDynamicAssembly(an, AssemblyBuilderAccess.Save);
var mb = ab.DefineDynamicModule("BetaModule", "Beta.dll");
var tb = mb.DefineType("n.Beta", TypeAttributes.Public, typeof(System.ValueType));
var fb = tb.DefineField("DATA", typeof(string), FieldAttributes.Public | FieldAttributes.Literal);
fb.SetConstant("Beta");
tb.CreateType();
ab.Save("Beta.dll");
}
I am not sure that it is 100% correct, but its work. BTW, it will be great if some one would check it. Maybe i am make some mistakes?