在运行时创建类,序列化和反序列化,然后转换为接口问题
嗨,
我有以下代码:
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
?
为什么?我该如何解决它?
Hi,
I have the following code :
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);
}
This will create a class based on the typeName
and inheritFrom
parameters and then finally an object will be created and returned. inheritFrom
will point at a class that implements IMyInterface
.
It's possible to cast this object to a IMyInterface
if it's needed.
When we then serialize and de-serialize this object we will not be able to cast it to IMyInterface
anymore?
Why? And how could I solve it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题出在对象的序列化和反序列化中。当这个改变时,效果很好。该解决方案位于第三方产品中,因此我无法将其发布在这里。
The problem was in the serialization och deserialization of the object. When this was changed it worked great. The solution is in a third party product so I can´t post it here.