Get Simple Properties - CSharp System.Reflection

CSharp examples for System.Reflection:PropertyInfo

Description

Get Simple Properties

Demo Code


using System.Reflection;
using System.Linq;
using System.Collections.Generic;
using System;/*from w  ww  . ja va  2  s  . co 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.GetGenericTypeDefinition() == typeof(IEnumerable<>)))
            {
                type = type.GetGenericArguments()[0];
            }


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

Related Tutorials