C#中动态扩展一个新类型的实现

本文介绍了一种使用 C# 动态创建类型的方法,并扩展现有类型的属性。通过 CodeDom 和反射技术,实现了根据现有类型和需要扩展的属性动态生成新的类型。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System.Reflection;
using System.CodeDom.Compiler;
using System.Text;
using Microsoft.CSharp;

 

class sample

{

        protected Type BuildExtendType(Type sourceType, PropertyInfo[] extenders)
        {
            StringBuilder codeBuilder = new StringBuilder();
            CodeDomProvider comp = new  CSharpCodeProvider();
            CompilerParameters cp = new CompilerParameters();
            codeBuilder.Append("using System;");
            codeBuilder.Append("using Microsoft.JScript;");
            codeBuilder.Append("public class ExtendObject{");
            codeBuilder.Append(PrepareProperties(sourceType.GetProperties()));
            codeBuilder.Append(PrepareMappingProperties(extenders));
            codeBuilder.Append("}");
            cp.ReferencedAssemblies.Contains("System.dll");
            cp.ReferencedAssemblies.Add("Microsoft.JScript.dll"); 
            cp.GenerateExecutable = false;
            cp.GenerateInMemory = true;
            string code = codeBuilder.ToString();
            CompilerResults cr = comp.CompileAssemblyFromSource(cp, code);
            if (cr.Errors.HasErrors)
            {
                string description = string.Empty;
                foreach (CompilerError e in cr.Errors)
                {
                    description += e.ErrorText;
                }
                throw new Exception(description);
            }
            else
            {
                Assembly a = cr.CompiledAssembly;
                return a.GetType("ExtendObject");
            }
        }

        protected string PrepareProperties(PropertyInfo[] ps)
        {
            StringBuilder sProperties = new StringBuilder();
            foreach (PropertyInfo p in ps)
            {
                sProperties.Append("protected ");
                sProperties.Append(p.PropertyType.FullName);
                sProperties.Append(" ");
                sProperties.Append("_" + p.Name);
                sProperties.Append(";");

                sProperties.Append("public ");
                sProperties.Append(p.PropertyType.FullName);
                sProperties.Append(" ");
                sProperties.Append(p.Name);
                sProperties.Append("{get{return " + "_" + p.Name + ";}set{" + "_" + p.Name + "= value;}}");
            }
            return sProperties.ToString();           
          }

          protected string PrepareMappingProperties(PropertyInfo[] ps)
        {
            //Samples: ITAB_BILL_TEMP_NAME
            StringBuilder sProperties = new StringBuilder();
            foreach (PropertyInfo p in ps)
            {
                sProperties.Append("protected ");
                sProperties.Append(p.PropertyType.FullName);
                sProperties.Append(" ");
                sProperties.Append("_" + p.DeclaringType.Name.Substring(11) + "_" + p.Name);
                sProperties.Append(";");

                sProperties.Append("public ");
                sProperties.Append(p.PropertyType.FullName);
                sProperties.Append(" ");
                sProperties.Append(p.DeclaringType.Name.Substring(11) + "_" + p.Name);
                sProperties.Append("{get{return " + "_" + p.DeclaringType.Name.Substring(11) + "_" + p.Name + ";}set{" + "_" + p.DeclaringType.Name.Substring(11) + "_" + p.Name + " = value;}}");
            }
            return sProperties.ToString();
        }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值