序列化不接受不同的命名空间。为什么?

发布于 2024-12-02 08:29:11 字数 2955 浏览 1 评论 0原文

我终于找到了问题所在,但这毫无意义,这似乎不可能。这甚至可以修复吗?我认为命名空间的工作方式都是相同的,您可以通过输入 thenamespace.theclass 等来调用和访问来自不同命名空间的控件,这主要用于分类(无论如何对我来说)。

我的问题是序列化似乎不接受新的命名空间,它生成的 ResX 代码会出现错误,除非引用与项目命名空间位于同一命名空间中。

原始错误:

错误 1 ​​Resx 文件无效。无法加载 .RESX 文件中使用的类型 Namespace2.FileFiltering、WindowsFormsApplication1、Version=1.0.0.0、Culture=neutral、PublicKeyToken=null。确保必要的引用已添加到您的项目中。第 127 行,位置 5。 c:\users\aderic\documents\visual studio 2010\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.resx 127 5 WindowsFormsApplication1

我想要一个可重用的控件,但我不想用它制作 DLL 。 (我可以混淆它,但我不想让人们导入它并使用它)。我有 2 个类,它们可以正常编译,但因为我选择了不同的命名空间,唯一的编译错误涉及 ResX 找不到对象。这里有两个课程:

using System;
using System.Windows.Forms;
using System.Runtime.Serialization;

namespace Namespace2
{
    class TestObject : Control
    {
        System.Collections.Generic.List<Namespace2.FileFiltering> InternalExtensions = new System.Collections.Generic.List<Namespace2.FileFiltering>();

        [System.ComponentModel.DesignerSerializationVisibility(System.ComponentModel.DesignerSerializationVisibility.Content)]
        public System.Collections.Generic.List<Namespace2.FileFiltering> ExtensionList
        {
            get
            {
                return InternalExtensions;
            }
            set
            {
                InternalExtensions = value;
            }
        }

        public TestObject()
        {
            BackColor = System.Drawing.Color.Gray;       
        }
    }

    [Serializable]
    public class FileFiltering : ISerializable
    {
        String InternalFileType = "New File Type";
        String[] InternalExtensions = new String[] { "*.*" };

        public String FileType
        {
            get
            {
                return InternalFileType;
            }
            set
            {
                InternalFileType = value;
            }
        }
        public String[] Extensions
        {
            get
            {
                return InternalExtensions;
            }
            set
            {
                InternalExtensions = value;
            }
        }

        public FileFiltering()
        {

        }

        public FileFiltering(SerializationInfo info, StreamingContext context)
        {
            FileType = info.GetString("FileType");
            Extensions = (String[])info.GetValue("FileExtensions", typeof(String[]));


            //Debugging.
            Console.WriteLine("FileType is " + FileType);
            Console.WriteLine("First extension is " + Extensions[0]);
        }


        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("FileType", FileType);
            info.AddValue("FileExtensions", Extensions);
        }
    }
}

有谁知道这个问题的解决方法还是我只是被困住了?我已经为此工作了几个小时,终于在一段时间后让它序列化,属性正常工作以及一切,至少其中一些错误是我刚刚了解到它与与项目名称空间不同的名称空间不能很好地发挥作用。

I finally found the problem, but there is no way this is making sense, it just doesn't seem possible. Is this even fixable? I thought namespaces all worked the same, you could call and access controls from different namespaces by typing thenamespace.theclass ect, this was mainly used for categorizing (for me anyways).

My problem is serializing doesn't seem to accept new namespaces, the ResX code it generates is bugged unless the references are in the same namespace as the project namespace.

Original Error:

Error 1 Invalid Resx file. Could not load type Namespace2.FileFiltering, WindowsFormsApplication1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null which is used in the .RESX file. Ensure that the necessary references have been added to your project. Line 127, position 5. c:\users\aderic\documents\visual studio 2010\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.resx 127 5 WindowsFormsApplication1

I would like a reusable control but I don't want to make a DLL out of it. (I can obfuscate it but I'd rather not have people importing it and using it). I have 2 classes, they would compile normally but because I chose a different namespace, the only compile error deals with the ResX not finding the object. Here are the 2 classes:

using System;
using System.Windows.Forms;
using System.Runtime.Serialization;

namespace Namespace2
{
    class TestObject : Control
    {
        System.Collections.Generic.List<Namespace2.FileFiltering> InternalExtensions = new System.Collections.Generic.List<Namespace2.FileFiltering>();

        [System.ComponentModel.DesignerSerializationVisibility(System.ComponentModel.DesignerSerializationVisibility.Content)]
        public System.Collections.Generic.List<Namespace2.FileFiltering> ExtensionList
        {
            get
            {
                return InternalExtensions;
            }
            set
            {
                InternalExtensions = value;
            }
        }

        public TestObject()
        {
            BackColor = System.Drawing.Color.Gray;       
        }
    }

    [Serializable]
    public class FileFiltering : ISerializable
    {
        String InternalFileType = "New File Type";
        String[] InternalExtensions = new String[] { "*.*" };

        public String FileType
        {
            get
            {
                return InternalFileType;
            }
            set
            {
                InternalFileType = value;
            }
        }
        public String[] Extensions
        {
            get
            {
                return InternalExtensions;
            }
            set
            {
                InternalExtensions = value;
            }
        }

        public FileFiltering()
        {

        }

        public FileFiltering(SerializationInfo info, StreamingContext context)
        {
            FileType = info.GetString("FileType");
            Extensions = (String[])info.GetValue("FileExtensions", typeof(String[]));


            //Debugging.
            Console.WriteLine("FileType is " + FileType);
            Console.WriteLine("First extension is " + Extensions[0]);
        }


        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("FileType", FileType);
            info.AddValue("FileExtensions", Extensions);
        }
    }
}

Does anyone know a workaround for this or am I just stuck? I've worked hours on this, finally after awhile got it to serialize, the properties working and everything, at least a few of those errors were me just learning that it doesn't play nice with a namespace different from the project namespace.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

请叫√我孤独 2024-12-09 08:29:11

该问题与名称空间本身无关。简而言之,框架在尝试反序列化 Control 时无法找到该类。

当框架尝试反序列化该属性时,可以通过指定要使用的自定义序列化器来克服这个问题 - 这可以通过 DesignerSerializerAttribute 来完成。

设计器序列化的文档:
http://msdn.microsoft.com/en-us/library/ms171834.aspx老实说

,我会完全避免设计器序列化,除非您有特定的需要(特别是因为您提到您对让其他开发人员重用您的控件不感兴趣)。

The problem has nothing to do with namespaces per-se. Simply that the framework can't locate the class when it's trying to deserialize the Control.

This can be overcome by specifying a custom serializer to use when the framework tries to deserialize that property - which can be accomplished with the DesignerSerializerAttribute.

The documentation for Designer Serialization:
http://msdn.microsoft.com/en-us/library/ms171834.aspx

Honestly, I'd avoid designer serialization altogether unless you have a specific need for it (especially since you mention you're not interested in having other developers reuse your control).

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