2017-01-20 55 views
4

我特別提請注意無效傳播,因爲它屬於bool?並使用返回方法bool。例如,考慮以下因素:空傳播爲什麼不一致地傳播Nullable <T>?

public static bool IsAttributedWith<TAttribute>(this JsonProperty property) 
    where TAttribute : Attribute 
{ 
    return property?.AttributeProvider 
        .GetAttributes(typeof(TAttribute), false) 
        .Any(); 
} 

這並不編譯,並且下面的錯誤存在:

無法隱式轉換布爾?布爾。存在明確的轉換(您是否缺少演員表)?

這意味着它是治療方法的整個身體作爲bool?,因爲這樣我會承擔,我可以說,.Any().GetValueOrDefault()但這是不允許的.Any()回報boolbool?

我知道我可以做到以下幾點作爲一個變通之一:

public static bool IsAttributedWith<TAttribute>(this JsonProperty property) 
    where TAttribute : Attribute 
{ 
    return property?.AttributeProvider 
        .GetAttributes(typeof(TAttribute), false) 
        .Any() 
     ?? false; 
} 

或者

public static bool IsAttributedWith<TAttribute>(this JsonProperty property) 
    where TAttribute : Attribute 
{ 
    var any = 
     property?.AttributeProvider 
       .GetAttributes(typeof(TAttribute), false) 
       .Any(); 

    return any.GetValueOrDefault(); 
} 

或者

public static bool IsAttributedWith<TAttribute>(this JsonProperty property) 
    where TAttribute : Attribute 
{ 
    return property?.AttributeProvider 
        .GetAttributes(typeof(TAttribute), false) 
        .Any() 
     ?? false; 
} 

我的問題是,爲什麼我不能直接調用.GetValueOrDefault()鏈接調用.Any()

public static bool IsAttributedWith<TAttribute>(this JsonProperty property) 
    where TAttribute : Attribute 
{ 
    return (property?.AttributeProvider 
        .GetAttributes(typeof(TAttribute), false) 
        .Any()) 
        .GetValueOrDefault(); 
} 

我認爲這將是有意義的值實際上是bool?在這一點上,而不是bool

+1

你應該把括號,所以'.'操作已知的,其中條件調用鏈結尾:'(property?.AttributeProvider.GetAttributes(typeof(TAttribute),false).Any())。GetValueOrDefault()'。 – PetSerAl

+0

如果'property'爲null,則該方法將嘗試返回null。但是,它不能因爲返回類型是'bool',它不是可以爲空的類型。將返回類型更改爲'bool?'。 – Abion47

回答

5

經過?.後面的所有跟隨呼叫鏈解釋爲不僅有立即呼叫有條件。因此,此代碼:

property==null ? (bool?)null : property.AttributeProvider 
             .GetAttributes(typeof(TAttribute), false) 
             .Any() 
             .GetValueOrDefault() 

它會失敗,因爲Any()回報boolbool?:如果您添加GetValueOrDefault()

property?.AttributeProvider 
     .GetAttributes(typeof(TAttribute), false) 
     .Any() 

解釋爲

property==null ? (bool?)null : property.AttributeProvider 
             .GetAttributes(typeof(TAttribute), false) 
             .Any() 

。因此,你需要在這裏使用括號:

(property==null ? (bool?)null : property.AttributeProvider 
             .GetAttributes(typeof(TAttribute), false) 
             .Any()) 
             .GetValueOrDefault() 

同括號時需要您使用?.操作人員使用:

(property?.AttributeProvider 
      .GetAttributes(typeof(TAttribute), false) 
      .Any()) 
      .GetValueOrDefault() 
2

GetValueOrDefault調用正在執行Any()方法的返回,該方法返回bool。如果你想對整個身體的結果執行,你將不得不用圓括號包裝它。

return (property?.AttributeProvider 
       .GetAttributes(typeof(TAttribute), false) 
       .Any()) 
       .GetValueOrDefault(); 

空條件運算符是一個短路操作者,因此什麼正在嘗試的對象或其任何屬性或方法上執行將不被執行,如果對象爲空的點的右側。所以,爲了在整個語句上執行代碼,你必須以某種方式包裝它(括號或使用另一個對象)。

+0

我知道我可以括號。但這不是我要問的。 C#將整個語句當作'bool?'處理,但是我不能對它作爲'bool?'來操作。我知道它在做什麼,我在問爲什麼。 –