代码之家  ›  专栏  ›  技术社区  ›  quadroid

Resharper错误地声明IDictionary.Values始终非空

  •  1
  • quadroid  · 技术社区  · 7 年前

    我得到以下代码:

    public IEnumerator<trow>getEnumerator())
    {
    if(this._rows.values==null)返回Enumerable.Empty<Trow>().GetEnumerator();
    
    返回这个。_rows.values.getEnumerator();
    }
    < /代码> 
    
    

    Resharper告诉我,_rows.values总是非空的(这不是这样的情况,因为我使用的IDictionary的实现-PostSharpsadvisableDictionaryfrom herehttp://doc.postsharp.net/t_postsharp_patterns_ccollections_advisabledictionary_2(a>)倾向于在创建后立即返回空值(我想非常尴尬的实现-我不知道他们为什么这样实现)。

    我使用的IDictionary的任何实现——为什么Resharper希望实现以这种方式工作?没有人能保证这是真的。或者我有什么问题吗?

    按照注释中的要求:显示行为的最小实现(在返回Enumerable.Empty<Trow>().GetEnumerator()上设置断点。which is grayed out by resharper):。

    类程序 { 静态void main(string[]args) { var模型=新模型(); bool any=model.any(); } } [通知属性已更改] 公共类模型:IEnumerable<string> { private idictionary<string,string>字典; 公共模型() { dictionary=new advisabledictionary<string,string>(); } 公共IEnumerator<string>GetEnumerator()。 { if(dictionary.values==null)返回Enumerable.Empty<String>().GetEnumerator(); 返回dictionary.values.getEnumerator(); } IEnumerator IEnumerable.GetEnumerator()。 { 返回getEnumerator(); } } < /代码>

    此示例要求使用postsharp.patterns.modelpackage和postsharp许可证(有试用版)或交换advisabledictionaryfor a custom dictionary that returns null on thevalues.getter.

    resharper告诉我,rows.values总是非空的(这不是因为我使用的IDictionary的实现-postsharps AdvisableDictionary 从这里 http://doc.postsharp.net/t_postsharp_patterns_collections_advisabledictionary_2 )在创建之后往往返回空值(我想是非常尴尬的实现——我不知道他们为什么这样实现)。

    enter image description here

    我使用的IDictionary的任何实现——为什么Resharper希望实现以这种方式工作?没有人能保证这是真的。或者我有什么问题吗?

    按照注释中的要求:显示行为的最小实现(在 return Enumerable.Empty<TRow>().GetEnumerator() 由Resharper灰显):

     class Program
    {
        static void Main(string[] args)
        {
            var model = new Model();
    
            bool any = model.Any();
        }
    }
    
    [NotifyPropertyChanged]
    public class Model : IEnumerable<string>
    {
        private IDictionary<string, string> dictionary;
    
        public Model()
        {
            dictionary = new AdvisableDictionary<string, string>();
        }
    
        public IEnumerator<string> GetEnumerator()
        {
            if (dictionary.Values == null) return Enumerable.Empty<string>().GetEnumerator();
            return dictionary.Values.GetEnumerator();
        }
    
        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }
    

    此示例要求 PostSharp.Patterns.Model 软件包和Postharp许可证(有试用版)或交换 建议性的 对于返回空值的自定义字典, Values 吸气剂。

    2 回复  |  直到 7 年前
        1
  •  4
  •   xanatos    7 年前

    在更新中 referencesource 微软增加了一些涉及 IDictionary<,> :

    // Returns a collections of the values in this dictionary.
    ICollection<TValue> IDictionary<TKey, TValue>.Values {
        get {
            Contract.Ensures(Contract.Result<ICollection<TValue>>() != null);
            return default(ICollection<TValue>);
        }
    }
    

    从这份合同中可以清楚地看出 Values 绝不可以 null (请注意,它是 mustn't 不是 can't :-)的中断实现 IDictionary<,> 可以返回 无效的 它不会被运行时捕获)。

        2
  •  1
  •   Oliver Heil    7 年前

    它总是 true 因为 .Values 总是返回某些内容(值集合)。如果字典应为空,则值集合也为空。如果字典是 null ,会有一个 NullPointerException 因为值不引用任何对象。

    推荐文章