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

Get Properties of container class using .net reflection

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

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; }

}

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

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;
    }

您可以为采用父元素的 ValidateObjectFields 编写重载,这样您就可以访问包含 class.

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

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

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

我认为这将解决您的问题。