コード例 #1
0
        public static ConstructorInfo GetConstructor(Type type, ReflectionFlags reflectionFlags, IEnumerable <Type> parameterTypes)
        {
            Contract.Requires(type != null);
            Contract.Requires(parameterTypes != null);

            return(GetConstructors(type.GetTypeInfo(), reflectionFlags, parameterTypes).SingleOrDefault());
        }
コード例 #2
0
        public static PropertyInfo GetProperty(Type type, string propertyName, ReflectionFlags reflectionFlags)
        {
            Contract.Requires(type != null);
            Contract.Requires(propertyName.SafeHasContent());

            return(GetProperties(type.GetTypeInfo(), propertyName, reflectionFlags).SingleOrDefault());
        }
コード例 #3
0
        public static MethodInfo GetMethod(Type type, string methodName, ReflectionFlags reflectionFlags, IEnumerable <Type> parameterTypes)
        {
            Contract.Requires(type != null);
            Contract.Requires(methodName.SafeHasContent());

            return(GetMethods(type.GetTypeInfo(), methodName, reflectionFlags, parameterTypes).SingleOrDefault());
        }
コード例 #4
0
        public static FieldInfo GetField(Type type, string fieldName, ReflectionFlags reflectionFlags)
        {
            Contract.Requires(type != null);
            Contract.Requires(fieldName.SafeHasContent());

            return(GetFields(type.GetTypeInfo(), fieldName, reflectionFlags).SingleOrDefault());
        }
コード例 #5
0
        public static ConstructorInfo GetConstructor(Type type, ReflectionFlags reflectionFlags, params Type[] parameterTypes)
        {
            if (parameterTypes == null || parameterTypes.Length == 0)
            {
                return(GetDefaultConstructor(type, reflectionFlags));
            }

            return(GetConstructors(type.GetTypeInfo(), reflectionFlags, parameterTypes).SingleOrDefault());
        }
コード例 #6
0
ファイル: ReflectionTests.cs プロジェクト: hundker/AutoUml-2
        private static string ScanP(Type t, ReflectionFlags flags)
        {
            var x = ClassMemberScannerVisitor
                    .ScanProperties(t, flags)
                    .Select(a => a.Name)
                    .OrderBy(a => a);

            return(string.Join("\r\n", x));
        }
コード例 #7
0
        public static IReadOnlyList <PropertyUmlMember> ScanProperties(Type type, ReflectionFlags scanFlags)
        {
            IEnumerable <PropertyUmlMember> ScanProperties(ReflectionFlags additionalFlag, UmlMemberKind kind)
            {
                var flags = scanFlags & ReflectionFlags.AllPropertyVisibility;

                if (flags == 0)
                {
                    yield break;
                }
                if (!scanFlags.HasFlag(additionalFlag))
                {
                    yield break;
                }
                var r = BindingFlags.Public | BindingFlags.NonPublic;

                if (additionalFlag == ReflectionFlags.InstanceProperty)
                {
                    r |= BindingFlags.Instance;
                }
                else if (additionalFlag == ReflectionFlags.StaticProperty)
                {
                    r |= BindingFlags.Static;
                }

                foreach (var pi in type.GetProperties(r))
                {
                    //var a_canRead = pi.CanRead;
                    //var a_canWrite = pi.CanWrite;
                    var getterFlag = GetGetterFlag(pi.GetMethod);
                    var setterFlag = GetSetterFlag(pi.SetMethod);
                    if (H(flags, getterFlag) || H(flags, setterFlag))
                    {
                        yield return new PropertyUmlMember
                               {
                                   Group      = 10,
                                   Name       = pi.Name,
                                   Property   = pi,
                                   Kind       = kind,
                                   Visibility = GetVisibilityFromFlags(getterFlag | setterFlag),
                               }
                    }
                    ;
                }
            }

            var p1 = ScanProperties(ReflectionFlags.InstanceProperty, UmlMemberKind.Normal);
            var p2 = ScanProperties(ReflectionFlags.StaticProperty, UmlMemberKind.Static);

            return(p1.Concat(p2).ToArray());
        }
コード例 #8
0
 private static VisibilityFlag GetVisibilityFromFlags(ReflectionFlags f)
 {
     if (H(f, ReflectionFlags.PublicProperty | ReflectionFlags.PublicMethod))
     {
         return(VisibilityFlag.Public);
     }
     if (H(f, ReflectionFlags.ProtectedProperty | ReflectionFlags.ProtectedMethod))
     {
         return(VisibilityFlag.Protected);
     }
     if (H(f, ReflectionFlags.PrivateProperty | ReflectionFlags.PrivateMethod))
     {
         return(VisibilityFlag.Protected);
     }
     return(VisibilityFlag.None);
 }
コード例 #9
0
 private static ReflectionFlags GetMFlag(MethodInfo m, ReflectionFlags publicFlag,
                                         ReflectionFlags protectedFlag, ReflectionFlags privateFlag)
 {
     if (m == null)
     {
         return(0);
     }
     if (m.IsPrivate)
     {
         return(privateFlag);
     }
     if (m.IsPublic)
     {
         return(publicFlag);
     }
     return(protectedFlag);
 }
コード例 #10
0
 public static ConstructorInfo GetDefaultConstructor(Type type, ReflectionFlags reflectionFlags)
 {
     Contract.Requires(type != null);
     return(GetConstructors(type.GetTypeInfo(), reflectionFlags, EmptyTypes).SingleOrDefault());
 }
コード例 #11
0
        public static IEnumerable <PropertyInfo> GetProperties(Type type, ReflectionFlags reflectionFlags)
        {
            Contract.Requires(type != null);

            return(GetProperties(type.GetTypeInfo(), null, reflectionFlags));
        }
コード例 #12
0
        public static IEnumerable <MethodInfo> GetMethods(Type type, ReflectionFlags reflectionFlags)
        {
            Contract.Requires(type != null);

            return(GetMethods(type.GetTypeInfo(), null, reflectionFlags, null));
        }
コード例 #13
0
        public static IEnumerable <ConstructorInfo> GetConstructors(Type type, ReflectionFlags reflectionFlags)
        {
            Contract.Requires(type != null);

            return(GetConstructors(type.GetTypeInfo(), reflectionFlags, null));
        }
コード例 #14
0
        private static IEnumerable <PropertyInfo> GetProperties(TypeInfo typeInfo, string propertyName, ReflectionFlags reflectionFlags)
        {
            if (typeInfo == null)
            {
                return(Enumerable.Empty <PropertyInfo>());
            }

            var properties = new List <PropertyInfo>();

            while (typeInfo != null)
            {
                var propertiesToAdd = typeInfo.DeclaredProperties;

                propertiesToAdd = FilterOnName(propertiesToAdd, propertyName, reflectionFlags);
                propertiesToAdd = FilterOnInstanceAndStatic(propertiesToAdd, reflectionFlags);
                propertiesToAdd = FilterOnPublicAndNonPublic(propertiesToAdd, reflectionFlags);

                properties.AddRange(propertiesToAdd);

                if (reflectionFlags.HasFlag(ReflectionFlags.DeclaredOnly))
                {
                    break;
                }

                typeInfo = typeInfo.BaseType != null?typeInfo.BaseType.GetTypeInfo() : null;
            }

            return(properties);
        }
コード例 #15
0
        private static IEnumerable <FieldInfo> GetFields(TypeInfo typeInfo, string fieldName, ReflectionFlags reflectionFlags)
        {
            if (typeInfo == null)
            {
                return(Enumerable.Empty <FieldInfo>());
            }

            var fields = new List <FieldInfo>();

            while (typeInfo != null)
            {
                var fieldsToAdd = typeInfo.DeclaredFields;

                fieldsToAdd = FilterOnName(fieldsToAdd, fieldName, reflectionFlags);
                fieldsToAdd = FilterOnInstanceAndStatic(fieldsToAdd, reflectionFlags);
                fieldsToAdd = FilterOnPublicAndNonPublic(fieldsToAdd, reflectionFlags);

                fields.AddRange(fieldsToAdd);

                if (reflectionFlags.HasFlag(ReflectionFlags.DeclaredOnly))
                {
                    break;
                }

                typeInfo = typeInfo.BaseType != null?typeInfo.BaseType.GetTypeInfo() : null;
            }

            return(fields);
        }
コード例 #16
0
        private static IEnumerable <MethodInfo> GetMethods(TypeInfo typeInfo, string methodName, ReflectionFlags reflectionFlags, IEnumerable <Type> parameterTypes)
        {
            if (typeInfo == null)
            {
                return(Enumerable.Empty <MethodInfo>());
            }

            var methods = new List <MethodInfo>();

            while (typeInfo != null)
            {
                var methodsToAdd = typeInfo.DeclaredMethods;

                methodsToAdd = FilterOnName(methodsToAdd, methodName, reflectionFlags);
                // ReSharper disable once PossibleMultipleEnumeration
                methodsToAdd = FilterOnParameterTypes(methodsToAdd, parameterTypes);
                methodsToAdd = FilterOnInstanceAndStatic(methodsToAdd, reflectionFlags);
                methodsToAdd = FilterOnPublicAndNonPublic(methodsToAdd, reflectionFlags);

                methods.AddRange(methodsToAdd);

                if (reflectionFlags.HasFlag(ReflectionFlags.DeclaredOnly))
                {
                    break;
                }

                typeInfo = typeInfo.BaseType != null?typeInfo.BaseType.GetTypeInfo() : null;
            }

            return(methods);
        }
コード例 #17
0
        private static IEnumerable <PropertyInfo> FilterOnPublicAndNonPublic(IEnumerable <PropertyInfo> query, ReflectionFlags reflectionFlags)
        {
            var isPublic    = reflectionFlags.HasFlag(ReflectionFlags.Public);
            var isNonPublic = reflectionFlags.HasFlag(ReflectionFlags.NonPublic);

            ValidatePublicAndNonPublicBindingFlags(isPublic, isNonPublic);

            if (isPublic && !isNonPublic)
            {
                query = query.Where(x => (x.CanRead && x.GetMethod.IsPublic) || (x.CanWrite && x.SetMethod.IsPublic));
            }
            // ReSharper disable once ConditionIsAlwaysTrueOrFalse
            else if (!isPublic && isNonPublic)
            {
                // IsFamilyOrAssembly == protected internal
                // IsFamily == protected
                // IsAssembly == internal
                // IsPrivate == private
                query = query.Where(x =>
                                    (x.CanRead && (x.GetMethod.IsFamilyOrAssembly || x.GetMethod.IsFamily || x.GetMethod.IsAssembly || x.GetMethod.IsPrivate)) ||
                                    (x.CanWrite && (x.SetMethod.IsFamilyOrAssembly || x.SetMethod.IsFamily || x.SetMethod.IsAssembly || x.SetMethod.IsPrivate)));
            }

            return(query);
        }
コード例 #18
0
        private static IEnumerable <ConstructorInfo> GetConstructors(TypeInfo typeInfo, ReflectionFlags reflectionFlags, IEnumerable <Type> parameterTypes)
        {
            if (typeInfo == null)
            {
                return(Enumerable.Empty <ConstructorInfo>());
            }

            var constructors = new List <ConstructorInfo>();

            var constructorsToAdd = typeInfo.DeclaredConstructors;

            constructorsToAdd = FilterOnParameterTypes(constructorsToAdd, parameterTypes);
            constructorsToAdd = FilterOnPublicAndNonPublic(constructorsToAdd, reflectionFlags);

            constructors.AddRange(constructorsToAdd);

            return(constructors);
        }
コード例 #19
0
        public static Expression <Func <TArgument1, TArgument2, TArgument3, TArgument4, TArgument5, TObject> > New <TArgument1, TArgument2, TArgument3, TArgument4, TArgument5, TObject>(ReflectionFlags reflectionFlags, Type objectType = null)
        {
            objectType = objectType ?? typeof(TObject);
            var argument1Type       = typeof(TArgument1);
            var argument2Type       = typeof(TArgument2);
            var argument3Type       = typeof(TArgument3);
            var argument4Type       = typeof(TArgument4);
            var argument5Type       = typeof(TArgument5);
            var argument1Expression = Expression.Parameter(argument1Type, "a");
            var argument2Expression = Expression.Parameter(argument2Type, "b");
            var argument3Expression = Expression.Parameter(argument3Type, "c");
            var argument4Expression = Expression.Parameter(argument4Type, "d");
            var argument5Expression = Expression.Parameter(argument5Type, "e");
            var constructorInfo     = TypeReflection.GetConstructor(objectType, reflectionFlags, argument1Type, argument2Type, argument3Type, argument4Type, argument5Type);
            var newExpression       = Expression.New(constructorInfo, argument1Expression, argument2Expression, argument3Expression, argument4Expression, argument5Expression);
            var lambdaExpression    = Expression.Lambda <Func <TArgument1, TArgument2, TArgument3, TArgument4, TArgument5, TObject> >(newExpression, argument1Expression, argument2Expression, argument3Expression, argument4Expression, argument5Expression);

            return(lambdaExpression);
        }
コード例 #20
0
 private static bool H(ReflectionFlags a, ReflectionFlags b)
 {
     return((a & b) > 0);
 }
コード例 #21
0
        private static IEnumerable <FieldInfo> FilterOnInstanceAndStatic(IEnumerable <FieldInfo> query, ReflectionFlags reflectionFlags)
        {
            var isInstance = reflectionFlags.HasFlag(ReflectionFlags.Instance);
            var isStatic   = reflectionFlags.HasFlag(ReflectionFlags.Static);

            ValidateInstanceAndStaticBindingFlags(isInstance, isStatic);

            if (isInstance && !isStatic)
            {
                query = query.Where(x => !x.IsStatic);
            }
            // ReSharper disable once ConditionIsAlwaysTrueOrFalse
            else if (!isInstance && isStatic)
            {
                query = query.Where(x => x.IsStatic);
            }

            return(query);
        }
コード例 #22
0
        private static IEnumerable <T> FilterOnPublicAndNonPublic <T>(IEnumerable <T> query, ReflectionFlags reflectionFlags)
            where T : MethodBase
        {
            var isPublic    = reflectionFlags.HasFlag(ReflectionFlags.Public);
            var isNonPublic = reflectionFlags.HasFlag(ReflectionFlags.NonPublic);

            ValidatePublicAndNonPublicBindingFlags(isPublic, isNonPublic);

            if (isPublic && !isNonPublic)
            {
                query = query.Where(x => x.IsPublic);
            }
            // ReSharper disable once ConditionIsAlwaysTrueOrFalse
            else if (!isPublic && isNonPublic)
            {
                // IsFamilyOrAssembly == protected internal
                // IsFamily == protected
                // IsAssembly == internal
                // IsPrivate == private
                query = query.Where(x => x.IsFamilyOrAssembly || x.IsFamily || x.IsAssembly || x.IsPrivate);
            }

            return(query);
        }
コード例 #23
0
        // PRIVATE METHODS //////////////////////////////////////////////////
        #region Methods
        private static IEnumerable <T> FilterOnName <T>(IEnumerable <T> query, string name, ReflectionFlags reflectionFlags)
            where T : MemberInfo
        {
            if (String.IsNullOrWhiteSpace(name))
            {
                return(query);
            }

            var comparisonType = reflectionFlags.HasFlag(ReflectionFlags.IgnoreCase) ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal;

            query = query.Where(x => String.Equals(x.Name, name, comparisonType));
            return(query);
        }
コード例 #24
0
        private static IEnumerable <PropertyInfo> FilterOnInstanceAndStatic(IEnumerable <PropertyInfo> query, ReflectionFlags reflectionFlags)
        {
            var isInstance = reflectionFlags.HasFlag(ReflectionFlags.Instance);
            var isStatic   = reflectionFlags.HasFlag(ReflectionFlags.Static);

            ValidateInstanceAndStaticBindingFlags(isInstance, isStatic);

            if (isInstance && !isStatic)
            {
                query = query.Where(x => (x.CanRead && !x.GetMethod.IsStatic) || (x.CanWrite && !x.SetMethod.IsStatic));
            }
            // ReSharper disable once ConditionIsAlwaysTrueOrFalse
            else if (!isInstance && isStatic)
            {
                query = query.Where(x => (x.CanRead && x.GetMethod.IsStatic) || (x.CanWrite && x.SetMethod.IsStatic));
            }

            return(query);
        }
コード例 #25
0
        public static Expression <Func <TArgument, TObject> > New <TArgument, TObject>(ReflectionFlags reflectionFlags, Type objectType = null)
        {
            objectType = objectType ?? typeof(TObject);
            var argumentType       = typeof(TArgument);
            var argumentExpression = Expression.Parameter(argumentType, "a");
            var constructorInfo    = TypeReflection.GetConstructor(objectType, reflectionFlags, argumentType);
            var newExpression      = Expression.New(constructorInfo, argumentExpression);
            var lambdaExpression   = Expression.Lambda <Func <TArgument, TObject> >(newExpression, argumentExpression);

            return(lambdaExpression);
        }