Class 在运行时创建类,序列化和反序列化,然后转换到接口froblem
你好, 我有以下代码:Class 在运行时创建类,序列化和反序列化,然后转换到接口froblem,class,inheritance,interface,runtime,Class,Inheritance,Interface,Runtime,你好, 我有以下代码: public static object CreateTypedReport(string typeName, string inheritFrom) { DirectoryInfo dirInfo; CSharpCodeProvider c = new CSharpCodeProvider(); CompilerParameters cp = new CompilerParameters(); foreach (Assembly asm
public static object CreateTypedReport(string typeName, string inheritFrom)
{
DirectoryInfo dirInfo;
CSharpCodeProvider c = new CSharpCodeProvider();
CompilerParameters cp = new CompilerParameters();
foreach (Assembly asm in System.AppDomain.CurrentDomain.GetAssemblies())
{
if(!asm.FullName.StartsWith("ReportAssembly, Version=0.0.0.0"))
cp.ReferencedAssemblies.Add(asm.Location);
}
cp.CompilerOptions = "/t:library";
cp.GenerateInMemory = true;
dirInfo = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\MyApp\\ReportAssemblies\\");
if (!dirInfo.Exists)
dirInfo.Create();
cp.OutputAssembly = dirInfo.FullName + typeName + "Assembly";
cp.ReferencedAssemblies.Add(typeof(XtraReport).Assembly.Location);
//cp.OutputAssembly = typeName + "Assembly";
StringBuilder sb = new StringBuilder("");
sb.Append("using System;\n");
sb.Append("using MyNamespace.UI;\n");
sb.Append("namespace TypedReports { \n");
sb.Append("public class " + typeName + " : " + inheritFrom + "{ \n");
sb.Append("} \n");
sb.Append("}\n");
CompilerResults cr = c.CompileAssemblyFromSource(cp, sb.ToString());
if (cr.Errors.Count > 0)
{
MessageBox.Show("ERROR: " + cr.Errors[0].ErrorText, "Error evaluating cs code", MessageBoxButtons.OK, MessageBoxIcon.Error);
return null;
}
return cr.CompiledAssembly.CreateInstance("TypedReports." + typeName);
}
这将基于typeName
和inheritFrom
参数创建一个类,然后最终将创建并返回一个对象inheritFrom
将指向实现IMyInterface
的类
如果需要,可以将此对象强制转换为IMyInterface
然后,当我们序列化和反序列化此对象时,我们将无法再将其强制转换为IMyInterface
为什么??我该如何解决它呢?问题在于对象的序列化和反序列化。当这一点被改变时,效果非常好。解决方案在第三方产品中,所以我不能在这里发布