以下不會編譯。感覺應該是這樣。我想知道是否有解決問題的方法。如果可能的話,我寧願避免要求任何課程下降。C#任意轉換
public static Func<DP, R> CastDomain<D, R, DP>(this Func<D, R> function) {
return (DP dp) => {
if (dp is D) {
D d = (D)dp; // Compile error: cannot convert DP to D. But that's crazy as we are inside if (dp is D) {
return function(d);
} else {
// handle the error . . .
}
};
}
您可以通過鑄造物體,例如, '(d)(對象)dp'。我敢肯定,必須有一個重複的解釋爲什麼... –
埃裏克Lippert涵蓋了這個問題[在他的博客](https://ericlippert.com/2015/10/14/casts-and-type-parameters-do - 未混合/) –