以下はコンパイルされません。それはすべきだと感じます。問題の周りに道があるかどうか疑問に思っています。可能であれば、私はクラスから降りることを何も要求しないことを望むだろう。任意の違いはありません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'。理由を説明する重複が必要であると確信しています。 –
Eric Lippertはこの問題を正確にカバーしています[彼のブログ](https://ericlippert.com/2015/10/14/casts-and-type-parameters-do -not-mix /) –