Missing something in this method to get the Entity property

88 Views Asked by At
private String[] GetProperties(EContent_EcontentFields eContentField)
{
    List<String> list = new List<String>();
    Type fieldType = eContentField.GetType();
    var properties = fieldType.GetProperties();
    foreach (var prop in properties)
    {
        if (prop.MemberType == MemberTypes.Property)
        {
            if (prop.PropertyType.IsGenericType)
            {
                dynamic items = prop.GetValue(eContentField, null);
                foreach (var item in items)
                {
                    Type typeItem = item.GetType();
                    list.Add(item);
                }
            }
        }
    }

    return list.ToArray();
}

This is my method to retrieve an array of strings with the properties of my entity but the statement:

if (prop.PropertyType.IsGenericType)

It's always false. I've copy-pasted the code so maybe I'm missing something. Something that I can't see.

2

There are 2 best solutions below

1
On BEST ANSWER
var result = new List<string>();
var type = eContentField.GetType();
foreach (var prop in type.GetProperties())
{
     result.Add(prop.Name);
 }

 return result.ToArray();
}

This is about twice faster than your method, if you are interested in speed. Ps: if you change the foreach into a for loop, it wil be a bit faster, but not a lot :)

0
On

I found a solution to retrieve all the property names.

private String[] GetProperties(EContent_EcontentFields eContentField)
        {
            List<String> list = new List<String>();
            Type type = eContentField.GetType();
            var properties = type.GetProperties().Where(x => x.MemberType == MemberTypes.Property).Select(x => x.Name);
            foreach (var item in properties)
            {
                if(item != null)
                    list.Add(item.ToString());
            }  

            return list.ToArray();
        }