1
動的プロパティを動的にオブジェクトに追加したいと考えています。同様の質問がここで回答されている間、それはExpandoObject
を使用しています。実行時に動的プロパティを動的に追加する
Dynamically Add C# Properties at Runtime
は、上記の答えは、動的にプロパティを追加しますが、それは私の必要性をfullfilしません。 変数のプロパティを追加できます。
public static ExpandoObject Extend<T>(this T obj)
{
ExpandoObject eo = new ExpandoObject();
PropertyInfo[] pinfo = typeof(T).GetProperties();
foreach(PropertyInfo p in pinfo)
{
//now in here I want to get the fields and properties of the obj
//and add it to the return value
//p.Name would be the eo.property name
//and its value would be p.GetValue(obj);
}
eo.SomeExtension = SomeValue;
return eo;
}
グレートansweを:
これは、あなたがこれを行うことができますrありがとう。 –