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;
}
{
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;
}