| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270 | 
							- using System;
 
- using System.Collections.Generic;
 
- using System.IO;
 
- using System.Linq;
 
- using System.Reflection;
 
- using System.Runtime.Serialization;
 
- using System.Text;
 
- using System.Threading.Tasks;
 
- namespace ServiceStack
 
- {
 
-     public static class ReflectionExtensions
 
-     {
 
-         public static bool IsInstanceOf(this Type type, Type thisOrBaseType)
 
-         {
 
-             while (type != null)
 
-             {
 
-                 if (type == thisOrBaseType)
 
-                     return true;
 
-                 type = type.BaseType();
 
-             }
 
-             return false;
 
-         }
 
-         public static Type FirstGenericType(this Type type)
 
-         {
 
-             while (type != null)
 
-             {
 
-                 if (type.IsGeneric())
 
-                     return type;
 
-                 type = type.BaseType();
 
-             }
 
-             return null;
 
-         }
 
-         public static Type GetTypeWithGenericTypeDefinitionOf(this Type type, Type genericTypeDefinition)
 
-         {
 
-             foreach (var t in type.GetTypeInterfaces())
 
-             {
 
-                 if (t.IsGeneric() && t.GetGenericTypeDefinition() == genericTypeDefinition)
 
-                 {
 
-                     return t;
 
-                 }
 
-             }
 
-             var genericType = type.FirstGenericType();
 
-             if (genericType != null && genericType.GetGenericTypeDefinition() == genericTypeDefinition)
 
-             {
 
-                 return genericType;
 
-             }
 
-             return null;
 
-         }
 
-         public static PropertyInfo[] GetAllProperties(this Type type)
 
-         {
 
-             if (type.IsInterface())
 
-             {
 
-                 var propertyInfos = new List<PropertyInfo>();
 
-                 var considered = new List<Type>();
 
-                 var queue = new Queue<Type>();
 
-                 considered.Add(type);
 
-                 queue.Enqueue(type);
 
-                 while (queue.Count > 0)
 
-                 {
 
-                     var subType = queue.Dequeue();
 
-                     foreach (var subInterface in subType.GetTypeInterfaces())
 
-                     {
 
-                         if (considered.Contains(subInterface)) continue;
 
-                         considered.Add(subInterface);
 
-                         queue.Enqueue(subInterface);
 
-                     }
 
-                     var typeProperties = subType.GetTypesProperties();
 
-                     var newPropertyInfos = typeProperties
 
-                         .Where(x => !propertyInfos.Contains(x));
 
-                     propertyInfos.InsertRange(0, newPropertyInfos);
 
-                 }
 
-                 return propertyInfos.ToArray();
 
-             }
 
-             return type.GetTypesProperties()
 
-                 .Where(t => t.GetIndexParameters().Length == 0) // ignore indexed properties
 
-                 .ToArray();
 
-         }
 
-         public static PropertyInfo[] GetPublicProperties(this Type type)
 
-         {
 
-             if (type.IsInterface())
 
-             {
 
-                 var propertyInfos = new List<PropertyInfo>();
 
-                 var considered = new List<Type>();
 
-                 var queue = new Queue<Type>();
 
-                 considered.Add(type);
 
-                 queue.Enqueue(type);
 
-                 while (queue.Count > 0)
 
-                 {
 
-                     var subType = queue.Dequeue();
 
-                     foreach (var subInterface in subType.GetTypeInterfaces())
 
-                     {
 
-                         if (considered.Contains(subInterface)) continue;
 
-                         considered.Add(subInterface);
 
-                         queue.Enqueue(subInterface);
 
-                     }
 
-                     var typeProperties = subType.GetTypesPublicProperties();
 
-                     var newPropertyInfos = typeProperties
 
-                         .Where(x => !propertyInfos.Contains(x));
 
-                     propertyInfos.InsertRange(0, newPropertyInfos);
 
-                 }
 
-                 return propertyInfos.ToArray();
 
-             }
 
-             return type.GetTypesPublicProperties()
 
-                 .Where(t => t.GetIndexParameters().Length == 0) // ignore indexed properties
 
-                 .ToArray();
 
-         }
 
-         public const string DataMember = "DataMemberAttribute";
 
-         internal static string[] IgnoreAttributesNamed = new[] {
 
-             "IgnoreDataMemberAttribute",
 
-             "JsonIgnoreAttribute"
 
-         };
 
-         public static PropertyInfo[] GetSerializableProperties(this Type type)
 
-         {
 
-             var properties = type.IsDto()
 
-                 ? type.GetAllProperties()
 
-                 : type.GetPublicProperties();
 
-             return properties.OnlySerializableProperties(type);
 
-         }
 
-         private static List<Type> _excludeTypes = new List<Type> { typeof(Stream) };
 
-         public static PropertyInfo[] OnlySerializableProperties(this PropertyInfo[] properties, Type type = null)
 
-         {
 
-             var isDto = type.IsDto();
 
-             var readableProperties = properties.Where(x => x.PropertyGetMethod(nonPublic: isDto) != null);
 
-             if (isDto)
 
-             {
 
-                 return readableProperties.Where(attr =>
 
-                     attr.HasAttribute<DataMemberAttribute>()).ToArray();
 
-             }
 
-             // else return those properties that are not decorated with IgnoreDataMember
 
-             return readableProperties
 
-                 .Where(prop => prop.AllAttributes()
 
-                     .All(attr =>
 
-                     {
 
-                         var name = attr.GetType().Name;
 
-                         return !IgnoreAttributesNamed.Contains(name);
 
-                     }))
 
-                 .Where(prop => !_excludeTypes.Contains(prop.PropertyType))
 
-                 .ToArray();
 
-         }
 
-     }
 
-     public static class PlatformExtensions //Because WinRT is a POS
 
-     {
 
-         public static bool IsInterface(this Type type)
 
-         {
 
-             return type.GetTypeInfo().IsInterface;
 
-         }
 
-         public static bool IsGeneric(this Type type)
 
-         {
 
-             return type.GetTypeInfo().IsGenericType;
 
-         }
 
-         public static Type BaseType(this Type type)
 
-         {
 
-             return type.GetTypeInfo().BaseType;
 
-         }
 
-         public static Type[] GetTypeInterfaces(this Type type)
 
-         {
 
-             return type.GetTypeInfo().ImplementedInterfaces.ToArray();
 
-         }
 
-         internal static PropertyInfo[] GetTypesPublicProperties(this Type subType)
 
-         {
 
-             var pis = new List<PropertyInfo>();
 
-             foreach (var pi in subType.GetRuntimeProperties())
 
-             {
 
-                 var mi = pi.GetMethod ?? pi.SetMethod;
 
-                 if (mi != null && mi.IsStatic) continue;
 
-                 pis.Add(pi);
 
-             }
 
-             return pis.ToArray();
 
-         }
 
-         internal static PropertyInfo[] GetTypesProperties(this Type subType)
 
-         {
 
-             var pis = new List<PropertyInfo>();
 
-             foreach (var pi in subType.GetRuntimeProperties())
 
-             {
 
-                 var mi = pi.GetMethod ?? pi.SetMethod;
 
-                 if (mi != null && mi.IsStatic) continue;
 
-                 pis.Add(pi);
 
-             }
 
-             return pis.ToArray();
 
-         }
 
-         public static bool HasAttribute<T>(this Type type)
 
-         {
 
-             return type.AllAttributes().Any(x => x.GetType() == typeof(T));
 
-         }
 
-         public static bool HasAttribute<T>(this PropertyInfo pi)
 
-         {
 
-             return pi.AllAttributes().Any(x => x.GetType() == typeof(T));
 
-         }
 
-         public static bool IsDto(this Type type)
 
-         {
 
-             if (type == null)
 
-                 return false;
 
-             return type.HasAttribute<DataContractAttribute>();
 
-         }
 
-         public static MethodInfo PropertyGetMethod(this PropertyInfo pi, bool nonPublic = false)
 
-         {
 
-             return pi.GetMethod;
 
-         }
 
-         public static object[] AllAttributes(this PropertyInfo propertyInfo)
 
-         {
 
-             return propertyInfo.GetCustomAttributes(true).ToArray();
 
-         }
 
-         public static object[] AllAttributes(this PropertyInfo propertyInfo, Type attrType)
 
-         {
 
-             return propertyInfo.GetCustomAttributes(true).Where(x => attrType.IsInstanceOf(x.GetType())).ToArray();
 
-         }
 
-         public static object[] AllAttributes(this Type type)
 
-         {
 
-             return type.GetTypeInfo().GetCustomAttributes(true).ToArray();
 
-         }
 
-         public static TAttr[] AllAttributes<TAttr>(this PropertyInfo pi)
 
-         {
 
-             return pi.AllAttributes(typeof(TAttr)).Cast<TAttr>().ToArray();
 
-         }
 
-         public static TAttr[] AllAttributes<TAttr>(this Type type)
 
-             where TAttr : Attribute
 
-         {
 
-             return type.GetTypeInfo().GetCustomAttributes<TAttr>(true).ToArray();
 
-         }
 
-     }
 
- }
 
 
  |