2011-06-01 81 views
1

在我的項目中,我有兩個庫,目前會導致循環依賴,我無法解決。C#如何解決由EditorAttribute使用引起的循環依賴?

一個庫爲整個解決方案提供了常見的數據結構。這個庫包含一個類似的結構:

namespace Common { 
    public class Foo { 
    //[Editor(typeof(UserEditor), typeof(UITypeEditor))] 
    public UInt32 OwnerId { get; set; } 
    } 

    public class User { 
    public UInt32 Id { get; set; } 
    } 
} 
在我們的解決方案的項目之一

現在,我們想通過一個PropertyGrid中和OwnerId屬性編輯Foo情況下應該用自定義編輯器進行編輯;這一個:

using Common; 

namespace Common.Gui { 
    public class OwnerEditor : UITypeEditor { 
    public static List<User> Users { get; set; } 
    } 
} 

現在,我不能只是添加EditorAttributeFoo.OwnerId,因爲這將創建一個循環依賴,我想保留出CommonCommon.Gui引用反正。我還想避免將Common中的代碼編入一個新的程序集中,因爲很多其他項目都參考了它。

我發現一個關於adding EditorAttribute at runtime的問題,這聽起來像是一個完美的解決方案,但我無法適應我的問題。

回答

2

我認爲這是一個有缺陷的設計的標誌。您是否想要包含Foo的類庫不知道編輯GUI?在這種情況下,它不應該包含EditorAttribute。

一種解決方案可能是將Foo類作爲MVVM體系結構中的模型。然後,您可以在應用EditorAttribute的GUI類庫中創建一個包裝ViewModel。如果你提取一個接口IFoo,我把它放在你的模型類庫中,你可以使用裝飾器模式。

2

您可以通過名稱引用編輯器類來創建運行時引用。

public class Foo { 
    [Editor("Common.Gui.OwnerEditor, Common", typeof(UITypeEditor))] 
    public UInt32 OwnerId { get; set; } 
    } 
+0

但是,我同意安德斯的回答。你的通用模塊仍然知道你的GUI模塊,所以它不是一個好的設計。 – 2011-06-01 10:42:08

+0

+1:你可以看到所有關於* .Design.dll的MS庫。 – leppie 2011-06-01 10:42:44

+0

實際上,我試圖在最初解決依賴關係,但它導致編輯器不能在PropertyGrid中使用。編輯:我的錯誤,這是由錯字造成的。這個建議現在工作正常。 – 2011-06-01 10:50:31