从模型中的类中获取属性

时间:2016-02-29 18:12:47

标签: c# asp.net asp.net-mvc

我将推送类属性的一种方法导入Error:(36, 32) No Json serializer found for type T. Try to implement an implicit Writes or Format for this type. val itemJson = Json.toJson(item) ^

NameValuCollection

在我的情况下,当我将My Model类传递给此方法时,它是正确的,但在我的Model类中,我使用另一个这样的模型

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

我怎么做?

1 个答案:

答案 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());
                    }
                }
            }
        }
    }

要检测自定义类型,我使用原始类型检查,包括stringdatetimeguid等特殊类型。但这会让你了解如何做到这一点。