dimanche 25 janvier 2015

Get Properties of container class using .net reflection

I have a class contains set of properties, one of the properties is a class type as below :



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

}


i have a generic method [because i have set of classes] to validate the properties and i use reflection to get props name and value dynamically and its working fine, but the problem is when it validates general_setup property it gets its props and start validating them. based on my business rules if it string.empty i want to set [code and message] props of the container class and i can not get this props at this level. any ideas? thanks



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





Aucun commentaire:

Enregistrer un commentaire