获取从视图模型获取财产 [英] get Get property from ViewModel
本文介绍了获取从视图模型获取财产的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有推类属性的一种方法为NameValuCollection
I have one method for push class property into 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;
}
在我的情况下,当我通过我的模型类,这种方法它是正确的,但在我的模型类我用这样的另一种模式
in my case when I pass My Model class to this method it's for correctly,but when in my Model class I use another Model like this
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; }
}
这不是加元标记类属性的集合,只是添加元标记到集合
it's not add MetaTags Class Property to the collection and just Add MetaTag to the collection
我想这个方法返回此输出
I want this method return this OutPut
key:Title Value:value
key:Description Value:value
key:Language Value:value
key:BrandName Value:value
但这种方法返回此
but this method return this
key:MetaTag Value:
key:BrandName Value:value
我可怎么办呢?
对你的帮助非常感谢
how i can do it ? very thank for your help
推荐答案
添加一个空字符串之前,检查当前属性是否是元标记
与否。如果是这样,递归使用此功能。
Before adding a empty string, check whether the current property is MetaTags
or not. If so, use this function recursively.
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.PropertyType == typeof(MetaTags))
{
parameter.Add(property.Name.ToString(),ObjectToCollection(property.GetValue(objects, null)))
}
else{
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;
}
这篇关于获取从视图模型获取财产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文