c# 微软企业库反射生成对象

public static TInterface Create<TInterface>()
        {
            Type interfaceType = typeof(TInterface);
            //object obj = DataCache.GetCache(typeof(TInterface).Name);
            ICacheManager cache = CacheFactory.GetCacheManager();
            object obj = cache.GetData(typeof(TInterface).Name);

            if (obj == null)
            {
                string[] tmp = InterfaceManager.RelationSettings[typeof(TInterface).Name].Split(',');
                string bllAssemblyName = tmp[1].Trim();
                string bllTypeFullName = tmp[0].Trim();
                Type bllType = Assembly.Load(bllAssemblyName).GetType(bllTypeFullName);   // 反射创建
                Type policyType = typeof(PolicyInjection);

                //get the code to compile

                // 1.Create a new CSharpCodePrivoder instance
                CSharpCodeProvider objCSharpCodePrivoder = new CSharpCodeProvider();

                // 2.Sets the runtime compiling parameters by crating a new CompilerParameters instance
                CompilerParameters objCompilerParameters = new CompilerParameters();
                objCompilerParameters.GenerateInMemory = true;
                objCompilerParameters.ReferencedAssemblies.Add("System.dll");
                objCompilerParameters.ReferencedAssemblies.Add("System.Configuration.dll");
                objCompilerParameters.ReferencedAssemblies.Add(policyType.Assembly.Location);
                objCompilerParameters.ReferencedAssemblies.Add(interfaceType.Assembly.Location);
                objCompilerParameters.ReferencedAssemblies.Add(bllType.Assembly.Location);

                // 3.CompilerResults: Complile the code snippet by calling a method from the provider
                StringBuilder sb = new StringBuilder();
                sb.Append("using System;");
                sb.Append("using System.Configuration;");
                sb.Append("using System.Reflection;");
                sb.Append(string.Format("using {0};", policyType.Namespace));
                sb.Append(string.Format("using {0};", interfaceType.Namespace));
                sb.Append(string.Format("using {0};", bllType.Namespace));

                sb.Append("namespace Dynamicly{");
                sb.Append("public class Program {");
                sb.Append("public object CreateObject() {");
                sb.Append(string.Format("object obj = PolicyInjection.Create<{0}, {1}>();", bllType.Name, interfaceType.Name));
                sb.Append("return obj;");
                sb.Append("}");
                sb.Append("}");
                sb.Append("}");
                string code = sb.ToString();
                CompilerResults cr = objCSharpCodePrivoder.CompileAssemblyFromSource(objCompilerParameters, code);

                // 4. Invoke the method by using Reflection
                Assembly objAssembly = cr.CompiledAssembly;
                object objClass = objAssembly.CreateInstance("Dynamicly.Program");

                obj = objClass.GetType().InvokeMember("CreateObject", BindingFlags.InvokeMethod, null, objClass, null);
                //DataCache.SetCache(typeof(TInterface).Name, obj);
                cache.Add(typeof(TInterface).Name, obj);
            }

            return (TInterface)obj;
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值