123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- /* INFINITY CODE 2013-2019 */
- /* http://www.infinity-code.com */
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Reflection;
- namespace InfinityCode.RealWorldTerrain.Utils
- {
- /// <summary>
- /// Helper class for compatibility of reflection on WSA.
- /// </summary>
- public static class RealWorldTerrainReflectionHelper
- {
- private const BindingFlags DefaultLookup = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public;
- /// <summary>
- /// Checks whether the type is anonymous.
- /// </summary>
- /// <param name="type">Type</param>
- /// <returns>True - type is anonymous, false - otherwise</returns>
- public static bool CheckIfAnonymousType(Type type)
- {
- if (type == null) throw new ArgumentNullException("type");
- return IsGenericType(type)
- && (type.Name.Contains("AnonymousType") || type.Name.Contains("AnonType"))
- && (type.Name.StartsWith("<>") || type.Name.StartsWith("VB$"))
- && (GetAttributes(type) & TypeAttributes.NotPublic) == TypeAttributes.NotPublic;
- }
- /// <summary>
- /// Gets the attributes associated with the Type.
- /// </summary>
- /// <param name="type">Type</param>
- /// <returns>Attributes of type.</returns>
- public static TypeAttributes GetAttributes(Type type)
- {
- #if !NETFX_CORE
- return type.Attributes;
- #else
- return type.GetTypeInfo().Attributes;
- #endif
- }
- /// <summary>
- /// Searches for the fields defined for the current Type, using the specified binding constraints.
- /// </summary>
- /// <param name="type">Type</param>
- /// <param name="bindingAttr">A bitmask comprised of one or more BindingFlags that specify how the search is conducted.</param>
- /// <returns>An array of FieldInfo objects representing all fields defined for the current Type that match the specified binding constraints.</returns>
- public static IEnumerable<FieldInfo> GetFields(Type type, BindingFlags bindingAttr = DefaultLookup)
- {
- #if !NETFX_CORE
- return type.GetFields(bindingAttr);
- #else
- return type.GetTypeInfo().DeclaredFields;
- #endif
- }
- /// <summary>
- /// Returns an array of Type objects that represent the type arguments of a generic type or the type parameters of a generic type definition.
- /// </summary>
- /// <param name="type">Type</param>
- /// <returns>An array of Type objects that represent the type arguments of a generic type. Returns an empty array if the current type is not a generic type.</returns>
- public static Type[] GetGenericArguments(Type type)
- {
- #if !NETFX_CORE
- return type.GetGenericArguments();
- #else
- return type.GetTypeInfo().GenericTypeArguments;
- #endif
- }
- /// <summary>
- /// Searches for the public members with the specified name.
- /// </summary>
- /// <param name="type">Type</param>
- /// <param name="name">The String containing the name of the public members to get. </param>
- /// <returns>An array of MemberInfo objects representing the public members with the specified name, if found; otherwise, an empty array.</returns>
- public static MemberInfo GetMember(Type type, string name)
- {
- #if !NETFX_CORE
- MemberInfo[] infos = type.GetMember(name);
- if (infos.Length > 0) return infos[0];
- return null;
- #else
- IEnumerable<MemberInfo> members = type.GetTypeInfo().DeclaredMembers;
- foreach (MemberInfo member in members) if (member.Name == name) return member;
- return null;
- #endif
- }
- /// <summary>
- /// searches for the members defined for the current Type, using the specified binding constraints.
- /// </summary>
- /// <param name="type">Type</param>
- /// <param name="bindingAttr">A bitmask comprised of one or more BindingFlags that specify how the search is conducted.</param>
- /// <returns>An array of MemberInfo objects representing all members defined for the current Type that match the specified binding constraints.</returns>
- public static IEnumerable<MemberInfo> GetMembers(Type type, BindingFlags bindingAttr = DefaultLookup)
- {
- #if !NETFX_CORE
- return type.GetMembers(bindingAttr);
- #else
- return type.GetTypeInfo().DeclaredMembers;
- #endif
- }
- /// <summary>
- /// Gets a MemberTypes value indicating the type of the member — method, constructor, event, and so on.
- /// </summary>
- /// <param name="member">MemberInfo</param>
- /// <returns>MemberTypes value</returns>
- public static MemberTypes GetMemberType(MemberInfo member)
- {
- #if !NETFX_CORE
- return member.MemberType;
- #else
- if (member is PropertyInfo) return MemberTypes.Property;
- if (member is FieldInfo) return MemberTypes.Field;
- if (member is MethodInfo) return MemberTypes.Method;
- if (member is EventInfo) return MemberTypes.Event;
- if (member is ConstructorInfo) return MemberTypes.Constructor;
- return MemberTypes.All;
- #endif
- }
- /// <summary>
- /// Searches for the public method with the specified name.
- /// </summary>
- /// <param name="type">Type</param>
- /// <param name="name">The String containing the name of the public method to get. </param>
- /// <returns>A MethodInfo object representing the public method with the specified name, if found; otherwise, null.</returns>
- public static MethodInfo GetMethod(Type type, string name)
- {
- #if !NETFX_CORE
- return type.GetMethod(name);
- #else
- return type.GetTypeInfo().GetDeclaredMethod(name);
- #endif
- }
- /// <summary>
- /// Searches for the specified public method whose parameters match the specified argument types.
- /// </summary>
- /// <param name="type">Type</param>
- /// <param name="name">The String containing the name of the public method to get. </param>
- /// <param name="types">An array of Type objects representing the number, order, and type of the parameters for the method to get.</param>
- /// <returns>A MethodInfo object representing the public method whose parameters match the specified argument types, if found; otherwise, null.</returns>
- public static MethodInfo GetMethod(Type type, string name, Type[] types)
- {
- #if !NETFX_CORE
- return type.GetMethod(name, types);
- #else
- var methods = type.GetTypeInfo().GetDeclaredMethods(name);
- foreach(var m in methods)
- {
- var parms = m.GetParameters();
- if (parms != null && parms.Length == types.Length && parms[0].ParameterType == typeof(string))
- {
- bool success = true;
- for(int i = 0; i < parms.Length; i++)
- {
- if (parms[i].ParameterType != types[i])
- {
- success = false;
- break;
- }
- }
- if (success) return m;
- }
- }
- return null;
- #endif
- }
- /// <summary>
- /// Returns all the public properties of the current Type.
- /// </summary>
- /// <param name="type">Type</param>
- /// <returns>An array of PropertyInfo objects representing all public properties of the current Type.</returns>
- public static PropertyInfo[] GetProperties(Type type)
- {
- #if !NETFX_CORE
- return type.GetProperties();
- #else
- return type.GetTypeInfo().DeclaredProperties.ToArray();
- #endif
- }
- /// <summary>
- /// Gets a value indicating whether the Type is a class; that is, not a value type or interface.
- /// </summary>
- /// <param name="type">Type</param>
- /// <returns>True if the Type is a class; otherwise, false.</returns>
- public static bool IsClass(Type type)
- {
- #if !NETFX_CORE
- return type.IsClass;
- #else
- return type.GetTypeInfo().IsClass;
- #endif
- }
- /// <summary>
- /// Gets a value indicating whether the current type is a generic type.
- /// </summary>
- /// <param name="type">Type</param>
- /// <returns>True if the current type is a generic type; otherwise, false.</returns>
- public static bool IsGenericType(Type type)
- {
- #if !NETFX_CORE
- return type.IsGenericType;
- #else
- return type.GetTypeInfo().IsGenericType;
- #endif
- }
- /// <summary>
- /// Gets a value indicating whether the Type is a value type.
- /// </summary>
- /// <param name="type">Type</param>
- /// <returns>True if the Type is a value type; otherwise, false.</returns>
- public static bool IsValueType(Type type)
- {
- #if !NETFX_CORE
- return type.IsValueType;
- #else
- return type.GetTypeInfo().IsValueType;
- #endif
- }
- }
- }
|