使用.net反射获取容器类的属性

时间:2015-01-25 08:08:05

标签: c# reflection

我有一个包含一组属性的类,其中一个属性是类类型 如下:

public class ProgramData
{
    public string code { get; set; }
    public string message { get; set; }

    public string program_id { get; set; }
    public string email { get; set; }
    public string first_name { get; set; }
    public string last_name { get; set; }

    public GeneralSetup general_setup { get; set; }
}

public class GeneralSetup
{
    public string store_name { get; set; }

    public bool store_enabled { get; set; }

    public bool promotions_enabled { get; set; }

    public bool barcode_scan_enabled { get; set; }

    public bool barcode_generate_enabled { get; set; }

}

我有一个通用方法[因为我有一组类]来验证属性,我使用反射来动态获取道具名称和值,并且它工作正常,但问题是当它验证general_setup属性时它获取它的道具和开始验证它们。 基于我的业务规则,如果它string.empty我想设置容器类的[代码和消息]道具,我不能在这个级别得到这个道具。 有任何想法吗?感谢

public T ValidateObjectFields<T>(T entity) where T : class
    {
        Type objType = entity.GetType();
        PropertyInfo[] properties = objType.GetProperties();
        foreach (PropertyInfo property in properties)
        {
            object propValue = property.GetValue(entity, null);
            var elems = propValue as IList;
            if (elems != null)
            {
                foreach (var item in elems)
                    ValidateObjectFields(item);
            }
            else
            {
                // Check if current property has sub object
                if (property.PropertyType.Assembly == objType.Assembly)
                {
                    #region Validate Objects

                        var code = objType.GetProperty("code");
                        var mesg = objType.GetProperty("message");


                        // in this case the property has sub object and i want to get these properties of container class
                        if (code == null && mesg == null) 
                        {
                            code = objType.GetProperty("code", BindingFlags.FlattenHierarchy | BindingFlags.Instance | BindingFlags.Public);
                            mesg = objType.GetProperty("message", BindingFlags.FlattenHierarchy | BindingFlags.Instance | BindingFlags.Public);
                        }

                        if (String.IsNullOrEmpty(Convert.ToString(propValue)))
                        {
                            //strDict = systemResponse.GetSystemResponse(Constants.structSystemResponses.Required_Field, //Constants.ConfigurableLanguages.English, Constants.enResponseSourceSystems.Webservice);
                            foreach (DictionaryEntry value in strDict)
                            {
                                code.SetValue(entity, Convert.ToString(value.Key), null);
                                mesg.SetValue(entity, Convert.ToString(value.Value) + " " + property.Name, null);
                            }
                            break;
                        }


                    #endregion

                    ValidateObjectFields(propValue);
                }
                else
                {
                    #region Validate Objects

                        var code = objType.GetProperty("code");
                        var mesg = objType.GetProperty("message");
                        // in this case the property has sub object and i want to get these properties of container class
                        if(code == null && mesg == null)
                        {
                            PropertyInfo[] info = objType.BaseType.GetProperties(BindingFlags.Public);

                            code = objType.GetProperty("code", BindingFlags.FlattenHierarchy | BindingFlags.Instance | BindingFlags.Public);
                            mesg = objType.GetProperty("message", BindingFlags.FlattenHierarchy | BindingFlags.Instance | BindingFlags.Public);
                        }

                        if (String.IsNullOrEmpty(Convert.ToString(propValue)))
                        {
                            strDict = systemResponse.GetSystemResponse(Constants.structSystemResponses.Required_Field, Constants.ConfigurableLanguages.English, Constants.enResponseSourceSystems.Webservice);
                            foreach (DictionaryEntry value in strDict)
                            {
                                code.SetValue(entity, Convert.ToString(value.Key), null);
                                mesg.SetValue(entity, Convert.ToString(value.Value) + " " + property.Name, null);
                            }
                            break;
                        }

                    #endregion
                }
            }
        }

        return entity;
    }

1 个答案:

答案 0 :(得分:1)

您可以为ValidateObjectFields编写一个重载,它接受一个父元素,这样您就可以访问包含类的属性。

public TEntity ValidateObjectFields<TEntity>(TEntity entity, object Entity)
{
   //Here put the code for handling the properties.
}

在上面的代码中调用此方法

foreach (var item in elems)
    ValidateObjectFields(item,entity);

这会解决我的问题。