1.动态生成程序集和类
2向动态类中添加字段和属性
3向动态类中添加方法
4.创建动态类的实例,并对字段和属性赋值
5将动态类序列化,并返回
//完成将partss、elevations、assemblyClassJson组合称一个类并反序列化
//要求根据assemblyClassJson生成的类要集成BridgeBusiness.GZW类
try
{
AssemblyName DemoName = new AssemblyName("DynamicAssembly");
AssemblyBuilder dynamicAssembly = AppDomain.CurrentDomain.DefineDynamicAssembly(DemoName, AssemblyBuilderAccess.RunAndSave);
//动态创建模块
ModuleBuilder mb = dynamicAssembly.DefineDynamicModule(DemoName.Name, DemoName.Name + ".dll");
JObject obj = (JObject)JsonConvert.DeserializeObject(assemblyClassJson);
string className = obj["Type"].ToString();
//动态创建类MyClass
TypeBuilder tb = mb.DefineType(className, TypeAttributes.Public | TypeAttributes.Class, typeof(BridgeBusiness.GZW));
//动态创建字段
// FieldBuilder fb = tb.DefineField("myField", typeof(System.String), FieldAttributes.Private);
//动态创建构造函数
//Type[] clorType = new Type[] {typeof(string) };
//ConstructorBuilder cb1 = tb.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, clorType);
生成指令
//ILGenerator ilg = cb1.GetILGenerator();//生成 Microsoft 中间语言 (MSIL) 指令
//ilg.Emit(OpCodes.Ldarg_0);
//ilg.Emit(OpCodes.Call, typeof(object).GetConstructor(Type.EmptyTypes));
//ilg.Emit(OpCodes.Ldarg_0);
// ilg.Emit(OpCodes.Ldarg_1);
//创建属性
//添加json
string objstr = obj["Data"].ToString();
var dic = JsonConvert.DeserializeObject<Dictionary<string, object>>(objstr);
foreach (var item in dic)
{
if (item.Value != null)
{
Type protype = item.Value.GetType();
// FieldBuilder fb = tb.DefineField(item.Key, protype, FieldAttributes.Public);
PropertyBuilder pb = tb.DefineProperty(item.Key, PropertyAttributes.HasDefault, protype, null);
pb.SetConstant(item.Value);
// fb.SetConstant(item.Value);
// ilg.Emit(OpCodes.Stfld, fb);
}
else
{
}
}
//添加零件集
for (int i = 0; i < this.partss.Count; i++)
{
Type type = partss[i].GetType();
PropertyInfo[] propertyInfos = type.GetProperties();
for (int j = 0; j < propertyInfos.Length; j++)
{
object objes = propertyInfos[j].GetValue(partss[i]);
Type proType = propertyInfos[j].PropertyType;
// FieldBuilder fb = tb.DefineField(propertyInfos[j].Name, proType, FieldAttributes.Public);
PropertyBuilder fb = tb.DefineProperty(propertyInfos[j].Name, PropertyAttributes.HasDefault, proType, null);
if (objes != null)
{
fb.SetConstant(objes);
}
else
{
fb.SetConstant(null);
}
}
}
//添加扩展属性
// private List<(string AttributeName, object)> extendedAttributes { get; set; } = new List<(string AttributeName, object)>()
foreach (var item in extendedAttributes)
{
string filedName = item.AttributeName.ToString();
Type proType = item.Item2.GetType();
List<(string ElevationName, double Value)> objes = (List < (string ElevationName, double Value) > )item.Item2;
PropertyBuilder fb1 = tb.DefineProperty(filedName, PropertyAttributes.HasDefault, proType, null);
if (objes.Count>0)
{
// fb1.SetConstant(objes);
fb1.SetValue(tb, objes);
}
}
//使用动态类创建类型
Type classType = tb.CreateType();
// dynamicAssembly.Save(DemoName.Name + ".dll");
object Vector = System.Activator.CreateInstance(classType);
string str= JsonConvert.SerializeObject(Vector);
return str;
// return null;
}
catch (Exception ex)
{
return ex.Message.ToString();
}