vendredi 13 octobre 2017

Reflection to loop through property type List`1

Hi all i have an object which may have a list as peroperty if it list i need to loop through the list also and need get all property of each object am stuck here can any one help me with this. Thanks in advance

public static string SerializeObject(object obj)
        {
            try
            {
                if (obj != null)
                {
                    Type objectType = obj.GetType();
                    foreach (PropertyInfo propertyInfo in objectType.GetProperties())
                    {
                        if (propertyInfo.CanRead)
                        {
                            var propertyName = propertyInfo.Name;
                            var propertyType = propertyInfo.PropertyType.Name;
                            if (propertyType.Contains("List"))
                            {
                                Console.WriteLine(propertyName);
    // here i need to loop through each object and need to check each property 
//and need to loop again if any more list came like a recursive function*** 
                            }
                            object firstValue = propertyInfo.GetValue(obj, null);
                        }
                    }
                }

                JavaScriptSerializer serializer = new JavaScriptSerializer();
                return serializer.Serialize(obj);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }





Aucun commentaire:

Enregistrer un commentaire