程序运行过程中动态生成程序集和类

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

            }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值