Get Simple Properties for Type - CSharp System.Reflection

CSharp examples for System.Reflection:PropertyInfo

Description

Get Simple Properties for Type

Demo Code


using System.Reflection;
using System.Linq;
using System.Collections.Generic;
using System;//from w ww  . j a va2 s. c  o m

public class Main{
        public static List<PropertyInfo> GetSimpleProperties(this Type type)
        {
            var properties = type.GetProperties();
            return properties.Where(c => c.PropertyType.IsSimpleType()).ToList();
        }
        public static bool IsSimpleType(this Type type)
        {
            if (type.IsGenericType && (type.GetGenericTypeDefinition() == typeof(Nullable<>) || type.GetGenericTypeDefinition() == typeof(List<>)))
            {
                type = type.GetGenericArguments()[0];
            }


            return type.IsPrimitive
                   || type.IsEnum
                   || type == typeof(string)
                   || type == typeof(DateTime)
                   || type == typeof(Version)
                   || type == typeof(Decimal);
        }
}

Related Tutorials