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; }
}
另一种模式它不添加MetaTag
阶级属性的集合,只是添加MetaTag
到集合
我希望这个方法返回此OutPut
key:Title Value:value
key:Description Value:value
key:Language Value:value
key:BrandName Value:value
但这种方法返回此
key:MetaTag Value:
key:BrandName Value:value
我该怎么办呢?
你需要让你的函数递归。 – SLaks
输出的目的是什么?你在做什么,你需要使用反射? –