私はDynamicObject
実装されたTryConvert
メソッドを持っています。次のコードはうまく機能します。
dynamic d = GetMyDynamic();
int i = (int)d; // TryConvert is called and returns the proper int value
ただし、d がオブジェクトにキャストされると、実行時に変換が失敗します。
object o = d;
int i = (int)o; // TryConvert is not called. InvalidCastException thrown
もちろん、(int)(dynamic)o
期待どおりに機能します。
何故ですか?そして、それを回避する方法はあり(int)o
ますTryConvert
か?