2017-09-07 42 views
0

我有以下功能如下用out PARAM,如果空將返回false,如果它不是空的TryGet模式,它將返回true通知resharper一個out變量只有在該函數返回false時才爲null;

​​

我然後調用該函數用下面的代碼:

IFileFormat fileFormatPlugin; 
if (_pluginLoader.TryGetFileFormat(extension, out fileFormatPlugin)) 
{ 
    fileFormatPlugin.DoStuff(); 

然後,resharper警告我fileFormatPlugin可能爲空。我該如何告訴resharper它只會在函數返回false時才爲空?

編輯我懷疑我可以用ContractAnnotation以某種方式處理這個問題,但不確定語法。

回答

1

以下合同註釋將使得ReSharper的,如果你使用的值當函數返回假的,只警告空引用

[ContractAnnotation("fileFormatter : null => false")] 
public bool TryGetFileFormat(string extension, [CanBeNull]out IFileFormat fileFormatter){ 
    fileFormatter = Plugins?.FirstOrDefault(plugin => plugin?.FileExtension != null && plugin.FileExtension.Equals(extension)); 
    return (fileFormatter != null); 
} 
相關問題