2017-09-07 8 views
0

나는이 그것을 반환 null가 아닌 경우는 null는, false를 돌려줍니다 경우 아웃 PARAM와 TryGet 패턴을 다음과 다음 함수 사실 내가 다음 코드로 함수를 호출 함수가 false를 반환하면 out 변수는 null이 될 것이라고 알려줍니다.

public bool TryGetFileFormat(string extension, [CanBeNull] out IFileFormat fileFormatter) 
{ 
    fileFormatter = Plugins?.FirstOrDefault(plugin => plugin?.FileExtension != null && plugin.FileExtension.Equals(extension)); 
    return (fileFormatter != null); 
} 

:

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

Resharper는 fileFormatPlugin이 null 일 수 있다고 경고합니다. 함수가 false를 반환하면 resharper에게 null이 될 것이라고 어떻게 말할 수 있습니까?

편집 제가 어떻게 든 ContractAnnotation으로 처리 할 수 ​​있지만 구문을 확신 할 수 없다고 생각합니다.

답변

1

다음 계약 주석은 함수가 거짓 돌아왔다 때 값을 사용하는 경우에만 null 참조에 대해 경고 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); 
}