2008年9月27日土曜日

クラスに定義してあるプロパティの呼び出し

System.ReflectionのGetPropertyを使うとプロパティを呼び出せる。
DLLにあるプロパティを呼ぶのに使用出来る。
メモ。

追記
読み返してみたら結局調べなおさないといけない。
めんどーなので追記する。

例)あるクラスのプロパティをあるクラスにコピーする
object obj = null;
object[] obj2 = null;
Type type = typeof(ClassA);
PropertyInfo[] properties = type.GetProperties();
foreach (PropertyInfo property in properties)
{
obj = type.InvokeMember(property.Name, BindingFlags.GetProperty, null, copyMotoClass, null);
obj2 = new object[1] { obj };
type.InvokeMember(property.Name, BindingFlags.SetProperty, null, copySakiClass, obj2);
}
ClassAはクラスの名前。
copyMotoClassはインスタンス化したクラスの変数名。コピー元。
copySakiClassはインスタンス化したクラスの変数名。コピー先。

MemberwiseClone()を使用しても良いけど、必要な場合もあるので。

0 コメント: