C#如何解决EditorAttribute使用引起的循环依赖关系
本文关键字:循环 关系 依赖 EditorAttribute 何解决 解决 | 更新日期: 2023-09-27 17:58:03
在我的项目中,我有两个库,它们当前会导致循环依赖,我无法解决。
一个库为整个解决方案提供了通用的数据结构。这个库包含一个类似的结构:
namespace Common {
public class Foo {
//[Editor( typeof( UserEditor ), typeof( UITypeEditor ) )]
public UInt32 OwnerId { get; set; }
}
public class User {
public UInt32 Id { get; set; }
}
}
现在,在我们解决方案中的一个项目中,我们希望通过PropertyGrid编辑Foo
实例,并且OwnerId
属性应该使用自定义编辑器进行编辑;这个:
using Common;
namespace Common.Gui {
public class OwnerEditor : UITypeEditor {
public static List<User> Users { get; set; }
}
}
现在,我不能只在Foo.OwnerId
中添加一个EditorAttribute
,因为这会创建一个循环依赖关系,而且我无论如何都希望在Common
中保留对Common.Gui
的引用。我还希望避免将代码从Common
中提取到一个新的程序集中,因为许多其他项目都引用了它
我发现了一个关于在运行时添加EditorAttribute
的问题,这听起来是一个完美的解决方案,但我无法熟练地解决这个问题。
我认为这是一个有缺陷的设计的标志。您希望包含Foo
的类库在编辑GUI时是未知的吗?在这种情况下,它不应该包含EditorAttribute。
一种解决方案可以是将Foo
类视为MVVM体系结构中的模型。然后,您可以在应用EditorAttribute
的GUI类库中创建一个包装ViewModel。如果你提取了一个接口IFoo,我把它放在你的模型类库中,你可以使用decorator模式。
您可以通过名称引用编辑器类来创建运行时引用。
public class Foo {
[Editor( "Common.Gui.OwnerEditor, Common", typeof( UITypeEditor ) )]
public UInt32 OwnerId { get; set; }
}