2016-02-29 78 views
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 

我该怎么办呢?

+1

你需要让你的函数递归。 – SLaks

+0

输出的目的是什么?你在做什么,你需要使用反射? –

回答

0

这里的递归方法给你所期望的结果:

public static NameValueCollection ObjectToCollection(object objects) 
    { 

     NameValueCollection parameter = new NameValueCollection(); 


     Type type = objects.GetType(); 

     PropertyInfo[] properties = type.GetProperties(BindingFlags.Instance | 
                 BindingFlags.DeclaredOnly | 
                 BindingFlags.Public); 
     foreach (var property in CollectPropertiesIncludingNestedTypes(objects, properties)) 
     { 
      parameter.Add(property.Item1, property.Item2); 
     } 

     return parameter; 
    } 

    private static IEnumerable<Tuple<string, string>> CollectPropertiesIncludingNestedTypes(object source, PropertyInfo[] properties) 
    { 
     foreach (PropertyInfo property in properties) 
     { 
      if (property.GetValue(source, null) == null) 
      { 
       yield return new Tuple<string, string>(property.Name.ToString(), ""); 
      } 
      else 
      { 
       var propValue = property.GetValue(source, null); 
       if (propValue.ToString() != "removeProp") 
       { 
        if (!(property.PropertyType.IsPrimitive || property.PropertyType == typeof(string) 
        || property.PropertyType == typeof(Guid) || property.PropertyType == typeof(DateTime))) 
        { 
         foreach (var val in CollectPropertiesIncludingNestedTypes(propValue, property.PropertyType.GetProperties(BindingFlags.Instance | 
                 BindingFlags.DeclaredOnly | 
                 BindingFlags.Public))) 
          yield return val; 
        } 
        else 
        { 
         yield return new Tuple<string, string>(property.Name.ToString(), propValue.ToString()); 
        } 
       } 
      } 
     } 
    } 

为了检测我使用了原始类型检查包括特殊类型,如stringdatetime和​​的自定义类型。但是这会给你一个如何完成的想法。

+0

非常感谢您的帮助 – jef