使用对象属性作为字典中的键
我想使用对象属性作为字典的键。这可以做到吗?
这样做的最终目标是使用它,以便可以在对象可能处于的各种状态下查看属性是否被锁定。这些锁定的值不会持久存在,仅存在于模型的业务规则中。
查看字段是否被锁定的理想代码如下所示;
bool ageLocked = myObject.IsFieldLocked( x => x.Age);
bool nameLocked = myObject.IsFieldLocked(x => x.Name);
IsFieldLocked 是 myObject 类型的扩展方法。
我希望字典位于 myObject 中,并且可以根据对象的状态替换为不同的字典变体,例如,已下订单或等待订单将具有不同的字典定义。
希望我能够使用工厂来创建不同的字典变体;
Factory.CreateAwaitingOrderLockedFields()
Factory.CreateOrderPlacedLockedFields()
定义看起来像这样的字典的
new Dictionary< ***MissingMagic***, bool>()
{
{ x => x.Age , true},
{ x => x.Name, false}
}
目的是避免键成为字符串,强类型键更可取。
I would like to use an objects property as the key for a dictionary. Can this be done?
The ultimate goal for this is to use this so can see if the property is locked or not, in various states that an object can be in. These locked value is not persisted, just exist in the business rules for the model.
Ideal code to see if field is locked would look like this;
bool ageLocked = myObject.IsFieldLocked( x => x.Age);
bool nameLocked = myObject.IsFieldLocked(x => x.Name);
IsFieldLocked being an extension method for the type of myObject.
I would like the dictionary to live in myObject and be replaceable with different dictionary variations based on the state of the object, for example, has placed an order or awaiting order would have different dictionary definitions.
Hopefully I would be able to use a factory to create the different dictionary variations;
Factory.CreateAwaitingOrderLockedFields()
Factory.CreateOrderPlacedLockedFields()
Defining the dictionary looking something like this
new Dictionary< ***MissingMagic***, bool>()
{
{ x => x.Age , true},
{ x => x.Name, false}
}
Aim is to avoid the key being a string, strongly typed key by far more desirable.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我将字典简单地定义为
Dictionary
。扩展方法可能如下所示:
I'd define the dictionary simply as a
Dictionary<string, bool>
.The extension method then could look something like this:
这是我根据 Herzmeister der welten 的建议简化的解决方案,
有了这个我可以执行以下操作;
Here is my cut down solution based on advice from herzmeister der welten
With this I can do the following;
我认为你应该只使用继承。创建基类LockedField,然后创建继承该类的AwaitingOrderLockedField 和OrderPlacedLockedField。
您的字典将是
IDictionary
I think you should just use inheritance. Create a base class LockedField then create AwaitingOrderLockedField and OrderPlacedLockedField that inherit this class.
You dictionary will be
IDictionary<LockedField, bool>
您需要在要用作字典中键的类中实现 IComparable 接口:
由于这实际上并不是对象成员的选项,因此您可以使用Dictionary以字段名称作为键,并在
Dictionary
DictionaryIsFieldLocked()
方法中进行一些反射,以从强类型字段中删除字符串。You will need to implement the IComparable interface in the class you want to use as key in a dictionary:
Since this is not really an option for a member of an object you may was well use
Dictionary<string, bool>
with the field name as the key and a bit of reflection in yourIsFieldLocked()
method to strip out the string from the strongly typed field.您可以将任何类型声明为键的字典;
将创建一个字典,其中表单元素用作键。
这是你问的吗?
如果要使用多个不同的对象作为键,您可以使用
Dictionary
或让所有对象继承另一个对象Dictionary
。You can declare the dictionary with any type as key;
would create a dictionary where form elements are used as key.
Is this What you where asking?
If several different objects are to be used as key you could use
Dictionary<object,bool>
or let all objects inherit from another objectDictionary<masterobject,bool>
.