I am trying to get class property name and its data type dynamically for any class so that I can build schema model required for Db e.g. Google BigQuery. I was able to get the string or int property name and Type but with array or list of custom class am not sure how to get their class property and name.
My model is :
public class StateModel
{
public string State { get; set; }
public string Gender { get; set; }
public int Year { get; set; }
public string Name { get; set; }
public int Number { get; set; }
public int[] Items { get; set; }
public List<string> Values { get; set; }
public Coordinate[] OrdinateArray { get; set; }
public List<Coordinate> Ordinates { get; set; }
public Coordinate CoordinateObj { get; set; }
}
public class Coordinate
{
public int Point { get; set; }
public string Value { get; set; }
}
My method to get class property is:
public static Dictionary<string, object> GetColumnFromClass<T>() where T : class, new()
{
Dictionary<string, object> fields = new Dictionary<string, object>();
T obj = new T();
var type = obj.GetType();
PropertyInfo[] properties = type.GetProperties();
foreach (var item in properties)
{
fields.Add(item.Name, item.PropertyType.Name.ToUpper())
}
return fields;
}
Can anyone help me how to get Coordinate class details i.e.(Point and Value name of Coordinate class) from the below properties:
public Coordinate[] OrdinateArray { get; set; }
public List<Coordinate> Ordinates { get; set; }
public Coordinate CoordinateObj { get; set; }
Note: I needed to get the subclass details dynamically and not hard coded.