在运行时创建类,序列化和反序列化,然后转换为接口问题

发布于 2024-12-08 14:00:13 字数 1873 浏览 0 评论 0原文

嗨,

我有以下代码:

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

这将根据 typeNameinheritFrom 参数创建一个类,然后最终创建一个对象并回来了。 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若沐 2024-12-15 14:00:13

问题出在对象的序列化和反序列化中。当这个改变时,效果很好。该解决方案位于第三方产品中,因此我无法将其发布在这里。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文