3
一般的なオブジェクトをNameValueCollectionに変換する必要があります。私は反射を使用しようとしています。私は親のプロパティを取得することができますが、私はオブジェクトである親のプロパティのプロパティを取得することはできません。ObjectをNameValueCollectionに変換する
Class One
public string name{get;set}
Class Two
public string desc{get;set}
public One OneName{get;set;}
public static NameValueCollection GetPropertyName(
string objType, object objectItem)
{
Type type = Type.GetType(objType);
PropertyInfo[] propertyInfos = type.GetProperties();
NameValueCollection propNames = new NameValueCollection();
foreach (PropertyInfo propertyInfo in objectItem.GetType().GetProperties())
{
if (propertyInfo.CanRead)
{
var pName = propertyInfo.Name;
var pValue = propertyInfo.GetValue(objectItem, null);
if (pValue != null)
{
propNames.Add(pName, pValue.ToString());
}
}
}
return propNames;
}
私はそこに再帰呼び出しのsomesortする必要があるが、それを行う方法を見つけ出すことはできませんと仮定します。どんな助けもありがとうございます。
'objType'は' objectItem'の型ですか?その場合は、そのパラメータを削除できます。また、使用しない 'propertyInfos'変数も作成しました。 – ThatMatthew
私は今すぐこのコードを使用することができました –