使用共享字典是解决缺乏“扩展属性”的好方法吗?
假设我有一些扩展方法,但还需要扩展对象的状态。鉴于 C# 中不支持扩展属性,使用共享静态字典是否是一个好的解决方案?
例如这样的事情:
class Foo
{
// 3rd party class
}
static class Helper
{
private static Dictionary<Foo, Guid> guidDict = new Dictionary<Foo, Guid>();
public static void DoSomething(this Foo foo)
{
Guid guid = guidDict[foo];
// do stuff
}
public static void DoAnotherthing(this Foo foo)
{
Guid guid = guidDict[foo];
// do stuff
}
}
还有哪些其他解决方案?
Suppose I have some extension methods but also need to extend the object's state. Seeing as there is no support for extension properties in C#, would using shared static Dictionary be a good solution?
For example something like this:
class Foo
{
// 3rd party class
}
static class Helper
{
private static Dictionary<Foo, Guid> guidDict = new Dictionary<Foo, Guid>();
public static void DoSomething(this Foo foo)
{
Guid guid = guidDict[foo];
// do stuff
}
public static void DoAnotherthing(this Foo foo)
{
Guid guid = guidDict[foo];
// do stuff
}
}
What are some other solutions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我不确定这是个好主意;同步将是一场噩梦,并且您需要使用一个不会冒使所有对象永远存活的风险的密钥(不要使用对象引用)。最好在对象内部使用属性包,或者将对象包装在提供缺少属性的其他内容中。您还可以使用继承,但这有更多限制(您可以封装密封类型或接口)。如果您确实想要的话,可以转发成员:
I'm not sure that is a good idea; the synchronization would be a nightmare, and you'd need to use a key that didn't risk keeping all the objects alive forever (don't use the object-reference). Better to use a property bag inside the object, or wrap your object in something else that provides the missing properties. You could also use inheritance, but that has more limitations (you can encapsulate a sealed type or an interface). You can forward the members if you really want:
如果您需要扩展对象的状态,那么我建议继承或组合。
If you need to extend an object's state then I'd recommend inheritance or composition.
您是正确的,您无法使用扩展方法维护状态,但也无法使用扩展属性维护状态。他们只能以您本来可以访问的方式操纵状态。
但是,我也不认为静态字典会有帮助。这可能有助于维护共享状态,但不是对象的状态。你正在做一些像下面这样奇特的事情吗?每个实例化的对象都有一个很好的唯一标识符,这样您就可以将一个状态变量添加到字典中,该变量将键入到该对象?这似乎有点迂回,如果这就是您尝试的
假设您无法控制类本身(因此需要以某种方式扩展它),您可以从这个对象继承吗?然后你当然可以做你需要做的事情。
You are correct in that you cannot maintain state with extension methods, but nor would you be able to with extension properties. They can only manipulate state in ways that you would otherwise have access to anyway.
However, I don't believe a static Dictionary will help either. It would be good possibly for maintaining shared state, but not an object's state. Are you doing something fancy like the following? There is a good unique identifier on each instantiated object, such that you could add a state variable to the dictionary that would be keyed to that object? That seems kindof round-about, if that's what you're attempting
Assuming you have no control over the class itself (hence the need to extend it in some way), can you inherit from this object? Then of course you can do what you need to do.
通常的继承或对象组合解决方案(装饰器模式)添加以下属性有什么问题你需要吗?
我不这么认为。除了同步问题和全局状态问题之外,您还存在丑陋的对象生命周期问题:
现在
o
超出了范围,但字典仍然有对它的引用!恶心!What's wrong with the usual solution of inheriting or object composition (decorator pattern) to add the properties that you need?
I don't think so. Aside from synchronization issues and global state issues, you also have ugly object lifetime issues:
Now
o
is out of scope but the dictionary still has a reference to it! Yucky!