0
獲取財產我有推類屬性的一個方法爲NameValuCollection得到從視圖模型
private NameValueCollection ObjectToCollection(object objects)
{
NameValueCollection parameter = new NameValueCollection();
Type type = objects.GetType();
PropertyInfo[] properties = type.GetProperties(BindingFlags.Instance |
BindingFlags.DeclaredOnly |
BindingFlags.Public);
foreach (PropertyInfo property in properties)
{
if (property.GetValue(objects, null) == null)
{
parameter.Add(property.Name.ToString(), "");
}
else
{
if (property.GetValue(objects, null).ToString() != "removeProp")
{
parameter.Add(property.Name.ToString(), property.GetValue(objects, null).ToString());
}
}
}
return parameter;
}
在我的情況下,當我通過我的模型類,以這種方法它是正確的,但是當我的模型類我用另一種型號這樣
public class Brand
{
public MetaTags MetaTag { get; set; } // <---- Problem is here
public string BrandName { get; set; }
}
public class MetaTags
{
public string Title { get; set; }
public string Description { get; set; }
public string Language { get; set; }
}
它不是加元標籤類屬性的集合,只是添加元標記到集合
我想這個方法返回此輸出
key:Title Value:value
key:Description Value:value
key:Language Value:value
key:BrandName Value:value
但這種方法返回此
key:MetaTag Value:
key:BrandName Value:value
我可怎麼辦呢? 非常感謝您的幫助
該代碼是「MetaTags」特有的。你也可以使它通用。 – SamGhatak
,感謝您的幫助,但是我在很多地方使用這種方法,我希望它適用於任何課程,而不僅僅是MetaTag,我該怎麼做? – jef
你可以在這種情況下檢查'property.GetValue(objects,null).GetType()。GetProperties(BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public).Length' 。 – SamGhatak