データベース呼び出しによって入力された ExpandoObjects で動作するコードがいくつかあります。値の一部は常に NULL です。オブジェクトを ExpandoObject として見ると、基になるディクショナリにすべてのキーと値 (null を含む) が表示されます。しかし、動的参照を介してそれらにアクセスしようとすると、対応する null 値を持つキーはオブジェクトの動的ビューに表示されません。動的参照のプロパティ構文を介してアクセスしようとすると、ArgumentNullException が発生します。
ExpandoObject を直接操作したり、一連の try キャッチを追加したり、expando を具象型にマッピングしたりすることで、これを回避できることはわかっていますが、それでは、そもそもこの動的オブジェクトを持つ目的が無効になります。動的オブジェクトを使用するコードは、一部のプロパティに null 値が含まれていても正常に機能します。null 値を持つこれらの動的プロパティを「再表示」する、より洗練された、または簡潔な方法はありますか?
これが私の「問題」を示すコードです
dynamic dynamicRef = new ExpandoObject();
ExpandoObject expandoRef = dynamicRef;
dynamicRef.SimpleProperty = "SomeString";
dynamicRef.NulledProperty = null;
string someString1 = string.Format("{0}", dynamicRef.SimpleProperty);
// My bad; this throws because the value is actually null, not because it isn't
// present. Set a breakppoint and look at the quickwatch on the dynamicRef vs.
// the expandoRef to see why I let myself be led astray. NulledProperty does not
// show up in the Dynamic View of the dynamicRef
string someString2 = string.Format("{0}", dynamicRef.NulledProperty);