C# 字典中对象的默认值
我有一本这样设置的词典:C# 字典中对象的默认值,c#,.net,dictionary,C#,.net,Dictionary,我有一本这样设置的词典:dictionary ItemProperties对象如下所示(基类是抽象的): 有没有一种方法可以像这样获得RenderedProp值(假设dictionary变量称为Properties): 对 string value = Properties[keyname].RenderedProp; 您可以使用自定义索引器方法创建自己的PropertyDictionary public class PropertyDictionary { Dictionary &l
dictionary
ItemProperties对象如下所示(基类是抽象的):
有没有一种方法可以像这样获得RenderedProp值(假设dictionary变量称为Properties):
对
string value = Properties[keyname].RenderedProp;
您可以使用自定义索引器方法创建自己的
PropertyDictionary
public class PropertyDictionary
{
Dictionary <string, StringProperty> dictionary;
public PropertyDictionary()
{
dictionary = new Dictionary <string, StringProperty>();
}
// Indexer; returns RenderedProp instead of Value
public string this[string key]
{
get { return dictionary[key].RenderedProp; }
set { dictionary[key].RenderedProp = value; }
}
}
公共类属性字典
{
字典;
公共财产词典
{
字典=新字典();
}
//索引器;返回renderdprop而不是值
公共字符串此[字符串键]
{
获取{return dictionary[key].RenderedProp;}
设置{dictionary[key].RenderedProp=value;}
}
}
您可以使用自定义索引器方法创建自己的PropertyDictionary
public class PropertyDictionary
{
Dictionary <string, StringProperty> dictionary;
public PropertyDictionary()
{
dictionary = new Dictionary <string, StringProperty>();
}
// Indexer; returns RenderedProp instead of Value
public string this[string key]
{
get { return dictionary[key].RenderedProp; }
set { dictionary[key].RenderedProp = value; }
}
}
公共类属性字典
{
字典;
公共财产词典
{
字典=新字典();
}
//索引器;返回renderdprop而不是值
公共字符串此[字符串键]
{
获取{return dictionary[key].RenderedProp;}
设置{dictionary[key].RenderedProp=value;}
}
}
否。如果要在字典中存储RenderedProp
值,只需将其设置为字典
,并适当添加即可。如果您确实需要字典中完整的ItemProperties
,但经常想要获得RenderedProp
,您可以创建一个方法来实现这一点(无论字典位于何处)
<>请注意,如果<代码> ReDeDeDePrp> <代码>只在<代码> StrugEngs/Cult>中指定(不在
字典
,并适当地添加它就行了。如果您确实需要字典中完整的ItemProperties
,但经常想要获得RenderedProp
,您可以创建一个方法来实现这一点(无论字典位于何处)
<>请注意,如果<代码> ReDeDeDePrp> <代码>只在<代码> StrugEngs/Cult>中指定(不在
StringProperty
到string
的隐式转换运算符,并将RenderedProp
返回给调用者:
public class StringProperty : ItemProperty
{
public string RawProp { get; set; }
public string RenderedProp { get; set; }
public static implicit operator string(StringProperty p)
{
return p.RenderedProp;
}
}
字典
需要使用StringProperty
,而不是ItemProperty
作为值类型,以便运算符应用。您的属性[keyname]也是如此。RenderedProp
代码也是如此。有一个解决方案,但我强烈建议不要这样做:定义一个从StringProperty
到string
的隐式转换运算符,并将RenderedProp
返回给调用者:
public class StringProperty : ItemProperty
{
public string RawProp { get; set; }
public string RenderedProp { get; set; }
public static implicit operator string(StringProperty p)
{
return p.RenderedProp;
}
}
字典
需要使用StringProperty
,而不是ItemProperty
作为值类型,以便运算符应用。您的属性[keyname]也是如此。RenderedProp
代码也是如此。您可以为字典实现扩展方法:
publicstaticintgetrp(这个字典dict,字符串键)
{
返回dict[key].RenderedProp;
}
不过,您必须直接调用它,而不必使用索引符号。如果使用较短的名称,则整个代码也同样较短。您可以为字典实现一个扩展方法:
publicstaticintgetrp(这个字典dict,字符串键)
{
返回dict[key].RenderedProp;
}
不过,您必须直接调用它,而不必使用索引符号。如果使用较短的名称,则整个代码同样较短。请相信此代码
string value=Properties[keyname].RenderedProp
get compile Error为什么您可能要这样做?在85%的情况下,我需要调用它,我只需要RenderdProp…但是有时我需要原始的..而不是一直调用RenderdProp,我认为只默认该属性会更干净相信这段代码string value=Properties[keyname].RenderedProp
get compile Error为什么您可能要这样做?在85%的情况下,我需要调用它,我只想要RenderdProp…但是有时我想要原始的..而不是一直调用RenderdProp,我认为默认为该属性InInteresting会更干净,为什么不建议这样想?@Scottingham我建议不要使用这种解决方案,因为像上面这样的隐式转换运算符会使读取依赖它们的代码变得更加困难。不熟悉您的代码的人在理解真正发生的事情之前会花一些时间挠头。这是一件坏事,尤其是当你处理大型代码库时。有趣的是,为什么不建议这样做呢?@Scottingham我反对这种解决方案,因为像上面这样的隐式转换运算符会使阅读依赖它们的代码变得更加困难。不熟悉您的代码的人在理解真正发生的事情之前会花一些时间挠头。这是一件坏事,尤其是当你处理大型代码库时。这就是我要寻找的…有没有理由不推荐或不好的做法?当你使用组合时,你必须重新实现所有的基本方法,比如Add()和Remove()。这不必是组合。如果你想把这些都整理出来,你可以从字典
继承;这只是索引器的一个示例。它似乎有
public static int GetRP(this Dictionary <string, ItemProperties> dict, string key)
{
return dict[key].RenderedProp;
}