2011-08-31 57 views
0

我終於找到了問題,但這是沒有辦法的,這似乎是不可能的。這是否可以修復?我認爲命名空間都是一樣的,你可以通過鍵入namespace.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); 
     } 
    } 
} 

有沒有人知道這個解決方法或我只是堅持?我已經在這上面花了幾個小時,終於在一段時間後將其序列化,屬性工作和一切,至少其中一些錯誤是我剛剛得知它不適用於與項目命名空間不同的命名空間。

+0

請爲您的問題使用適當的標題。 「我在我的智慧結局」並沒有描述你的問題。 –

+0

謝謝,我試圖想出一個標題並編輯這篇文章。這個標題很好。 –

+0

你得到的實際錯誤是什麼?你如何準確地序列化這些對象? – Rob

回答

1

該問題與命名空間本身無關。簡單地說,當它試圖反序列化Control時,框架無法找到該類。

這可以通過在框架試圖反序列化該屬性時指定自定義序列化程序來克服 - 可以使用DesignerSerializerAttribute來完成。

的設計師序列化的文檔: http://msdn.microsoft.com/en-us/library/ms171834.aspx

老實說,我會避免設計器序列共有,除非你有它的特殊需要(特別是因爲你提到你不是有意讓其他開發者重用你的控制)。

+0

我打算使用它很多..我的意思是很多.. 我通常浪費我的時間寫其他人的東西,即使我想讓他們自己。我想我最終會把所有東西都開源,我可能會從中學習。 不管怎麼說,我跟你說什麼,一切都以正確編譯: '[System.ComponentModel.Design.Serialization.DesignerSerializerAttribute(typeof運算(FileFiltering)的typeof(System.Runtime.Serialization.Formatters.Binary.BinaryFormatter))] ' 在我的對象的頂部。非常感謝幫助:D。 –

相關問題