Serialization doesn't accept different namespaces. Why?
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)
开发者_C百科 {
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.
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).
精彩评论