get Get property from ViewModel

前端 未结 1 940
说谎
说谎 2021-01-26 08:36

I have one method for push class property into NameValuCollection

private NameValueCollection ObjectToCollection(object objects)
{

    NameValueCollection param         


        
相关标签:
1条回答
  • 2021-01-26 09:07

    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;
    }
    
    0 讨论(0)
提交回复
热议问题