public static MethodInfo GetMethodEx([NotNull] this Type type, string name, MemberFlags flags = MemberFlags.Public | MemberFlags.Static | MemberFlags.Instance)
        {
            Should.NotBeNull(type, nameof(type));
            var bindingFlags = flags.ToBindingFlags(false);
            var method       = type.GetMethod(name, bindingFlags | BindingFlags.FlattenHierarchy);

            if (method != null || !flags.HasMemberFlag(MemberFlags.NonPublic))
            {
                return(method);
            }
            bindingFlags |= BindingFlags.DeclaredOnly;
            type          = type.BaseType;
            while (type != null)
            {
                var result = type.GetMethod(name, bindingFlags);
                if (result != null)
                {
                    if (method != null)
                    {
                        throw new AmbiguousMatchException();
                    }
                    method = result;
                }
                type = type == typeof(object) ? null : type.BaseType;
            }
            return(method);
        }
        public static ICollection <MethodInfo> GetMethodsEx([NotNull] this Type type, MemberFlags flags = MemberFlags.Public | MemberFlags.Static | MemberFlags.Instance)
        {
            Should.NotBeNull(type, nameof(type));
            var bindingFlags = flags.ToBindingFlags(false);

            if (flags.HasMemberFlag(MemberFlags.NonPublic))
            {
                bindingFlags |= BindingFlags.DeclaredOnly;
                var list = new List <MethodInfo>();
                while (type != null)
                {
                    list.AddRange(type.GetMethods(bindingFlags));
                    type = type == typeof(object) ? null : type.BaseType;
                }
                return(list);
            }
            return(type.GetMethods(bindingFlags | BindingFlags.FlattenHierarchy));
        }
        public static EventInfo GetEventEx([NotNull] this Type type, string name, MemberFlags flags = MemberFlags.Public | MemberFlags.Static | MemberFlags.Instance)
        {
            Should.NotBeNull(type, nameof(type));
            var bindingFlags = flags.ToBindingFlags(false);
            var @event       = type.GetEvent(name, bindingFlags | BindingFlags.FlattenHierarchy);

            if (@event != null || !flags.HasMemberFlag(MemberFlags.NonPublic))
            {
                return(@event);
            }
            bindingFlags |= BindingFlags.DeclaredOnly;
            type          = type.BaseType;
            while (type != null)
            {
                @event = type.GetEvent(name, bindingFlags);
                if (@event != null)
                {
                    return(@event);
                }
                type = type == typeof(object) ? null : type.BaseType;
            }
            return(null);
        }
Beispiel #4
0
 public static EventInfo GetEventEx(this Type sourceType, string eventName, MemberFlags flags = MemberFlags.Public | MemberFlags.Static | MemberFlags.Instance)
 {
     return(sourceType.GetEvent(eventName, flags.ToBindingFlags()));
 }
Beispiel #5
0
 public static MethodInfo[] GetMethodsEx([NotNull] this Type type, MemberFlags flags = MemberFlags.Public | MemberFlags.Static | MemberFlags.Instance)
 {
     Should.NotBeNull(type, "type");
     return(type.GetMethods(flags.ToBindingFlags()));
 }
Beispiel #6
0
 public static FieldInfo GetFieldEx([NotNull] this Type type, string name, MemberFlags flags = MemberFlags.Public | MemberFlags.Static | MemberFlags.Instance)
 {
     Should.NotBeNull(type, "type");
     return(type.GetField(name, flags.ToBindingFlags()));
 }