简体   繁体   中英

Class property name and its data type for sub class using Reflection

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 eg 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 ie(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.

Please see if this will meet your requirement (Same property name in parent and child will lead to only one entry of parent, this can be handled with a recursion depth parameter):

private static ConcurrentDictionary<string, object> GetColumnFromClass(object obj, ConcurrentDictionary<string, object> fields)
        {
            //null type will not be processed
            if (obj == null)
                return null;

            Type objType = obj.GetType();
            PropertyInfo[] properties = objType.GetProperties();

            foreach (PropertyInfo property in properties)
            {
                if (property.PropertyType.IsGenericType && property.PropertyType.GetGenericTypeDefinition() == typeof(List<>))
                {
                    Type itemType = property.PropertyType.GetGenericArguments()[0];
                    if (itemType == typeof(string))
                        fields.AddOrUpdate(property.Name, property.PropertyType.Name.ToUpper(), (k, o) => o);
                    else
                    {
                        fields.AddOrUpdate(property.Name, property.PropertyType.Name.ToUpper(), (k, o) => o);
                        GetColumnFromClass(Activator.CreateInstance(itemType), fields);
                    }
                }
                else
                {
                    object propVal = property.GetValue(obj, null);
                    if (property.PropertyType.Assembly == objType.Assembly)
                    {
                        fields.AddOrUpdate(property.Name, property.PropertyType.Name.ToUpper(), (k,o) => o);
                        GetColumnFromClass(propVal, fields);
                    }
                    else
                    {
                        fields.AddOrUpdate(property.Name, property.PropertyType.Name.ToUpper(), (k, o) => o);
                    }
                }
            }
            return fields;
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM