我终于找到了问题所在,但是这没有任何意义,只是似乎不可能。这甚至可以修复吗?我认为命名空间的工作原理都是一样的,您可以通过键入 thenamespace.theclass 等来调用和访问来自不同命名空间的控件,这主要用于分类(无论如何对我来说)。
我的问题是序列化似乎不接受新的命名空间,它生成的 ResX 代码被窃听,除非引用与项目命名空间位于同一命名空间中。
原始错误:
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
我想要一个可重用的控件,但我不想用它制作一个 DLL。 (我可以混淆它,但我宁愿没有人导入并使用它)。我有 2 个类,它们可以正常编译,但是因为我选择了不同的命名空间,唯一的编译错误是 ResX 没有找到对象。这是2个类:
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);
}
}
}
有谁知道解决这个问题的方法还是我只是卡住了?我已经为此工作了几个小时,终于过了一段时间让它序列化,属性工作和一切,至少其中一些错误是我刚刚了解到它在与项目命名空间不同的命名空间中表现不佳。
最佳答案
该问题与 namespace 本身无关。简单地说,框架在尝试反序列化控件时无法定位该类。
这可以通过在框架尝试反序列化该属性时指定要使用的自定义序列化器来克服 - 这可以通过 DesignerSerializerAttribute
完成。 .
Designer 序列化的文档:
http://msdn.microsoft.com/en-us/library/ms171834.aspx
老实说,除非您有特殊需要,否则我会完全避免设计器序列化(特别是因为您提到您对让其他开发人员重用您的控件不感兴趣)。
关于c# - 序列化不接受不同的命名空间。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7251945/