public partial class ReturnHeader
{
public int ReturnHeaderId { get; set; }
public int CustomerId { get; set; }
public string InvoiceNo { get; set; }
public virtual Customer Customer { get; set; }
public virtual ICollection<ReturnDetail> ReturnDetails { get; set; }
}
public void TraverseThroughClass<T> (T entity) where T : class
{
try
{
var type = typeof(T);
PropertyInfo[] props = type.GetProperties();
comparedEntityHashSets.Add(entity.GetHashCode());
foreach (PropertyInfo prop in props)
{
if (typeof(IEnumerable).IsAssignableFrom(prop.PropertyType)
&& !comparedEntityHashSets.Contains(prop.GetHashCode()))
{
This check determines whether the property is a list or not. Ignore comparedEntityHashSets. Issue 1: if true, cast it into list or any collection.
var propertiesValues = prop.GetValue(this) as IList;
var listType = GetCollectionItemType(prop.PropertyType);
foreach (var listItem in propertiesValues)
{
Issue 2:For each item in collection call TraverseThroughClass (T entity)
}
}
else
{
Console.WriteLine("Prop Name : " + prop.Name + " Prop Value : "
+ prop.GetValue(entity, null));
}
}
}
catch (Exception e)
{
throw;
}
}
public static Type GetCollectionItemType(Type collectionType)
{
var types = collectionType.GetInterfaces()
.Where(x => x.IsGenericType
&& x.GetGenericTypeDefinition() == typeof(IEnumerable<>))
.ToArray();
return types.Length == 1 ? types[0].GetGenericArguments()[0] : null;
}
Aucun commentaire:
Enregistrer un commentaire