static IEnumerable <MethodInfo> ApplyBindingFlags(this IEnumerable <MethodInfo> infos, BindingFlags flags)
        {
            bool pub  = flags.HasFlag(BindingFlags.Public);
            bool priv = flags.HasFlag(BindingFlags.NonPublic);

            if (priv && !pub)
            {
                infos = infos.Where(m => m.IsPrivate);
            }
            else if (pub && !priv)
            {
                infos = infos.Where(m => m.IsPublic);
            }

            bool stat = flags.HasFlag(BindingFlags.Static);
            bool inst = flags.HasFlag(BindingFlags.Instance);

            if (stat && !inst)
            {
                infos = infos.Where(m => m.IsStatic);
            }
            else if (inst && !stat)
            {
                infos = infos.Where(m => !m.IsStatic);
            }

            return(infos);
        }
Exemplo n.º 2
0
        private static IEnumerable <PropertyInfo> FilterOnPublicAndNonPublic(IEnumerable <PropertyInfo> query, BindingFlags bindingFlags)
        {
            var isPublic    = bindingFlags.HasFlag(BindingFlags.Public);
            var isNonPublic = bindingFlags.HasFlag(BindingFlags.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);
        }
Exemplo n.º 3
0
        private static bool Filter(TypeInfo Info, BindingFlags Flags)
        {
            bool Return = false;

            if (Flags.HasFlag(BindingFlags.DeclaredOnly))
            {
                Return |= Info.IsNested;
            }
            if (Flags.HasFlag(BindingFlags.Instance))
            {
                Return |= !(Info.IsAbstract | Info.IsSealed);
            }
            if (Flags.HasFlag(BindingFlags.Static))
            {
                Return |= Info.IsAbstract | Info.IsSealed;
            }
            if (Flags.HasFlag(BindingFlags.Public))
            {
                Return |= Info.IsPublic;
            }
            if (Flags.HasFlag(BindingFlags.NonPublic))
            {
                Return |= Info.IsNotPublic;
            }
            return(Return);
        }
Exemplo n.º 4
0
        protected object InvokeStandardMember(string name, BindingFlags invokeAttr, Binder binder, object target,
                                              object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] namedParameters)
        {
            BindingFlags processedInvokeAttr = invokeAttr;

            if ((processedInvokeAttr.HasFlag(BindingFlags.GetProperty) ||
                 processedInvokeAttr.HasFlag(BindingFlags.PutDispProperty)) &&
                !_properties.Any(p => p.Name == name) &&
                _fields.Any(p => p.Name == name))
            {
                if (processedInvokeAttr.HasFlag(BindingFlags.GetProperty))
                {
                    processedInvokeAttr &= ~BindingFlags.GetProperty;
                    processedInvokeAttr |= BindingFlags.GetField;
                }
                else if (processedInvokeAttr.HasFlag(BindingFlags.PutDispProperty))
                {
                    processedInvokeAttr &= ~BindingFlags.PutDispProperty;
                    processedInvokeAttr |= BindingFlags.SetField;
                }
            }

            object result = _type.InvokeMember(name, processedInvokeAttr, binder, target,
                                               args, modifiers, culture, namedParameters);

            return(result);
        }
Exemplo n.º 5
0
        public override bool HasMember(EvaluationContext ctx, object tt, string memberName, BindingFlags bindingFlags)
        {
            CorEvaluationContext cctx = (CorEvaluationContext)ctx;
            CorType ct = (CorType)tt;

            while (ct != null)
            {
                Type type = ct.GetTypeInfo(cctx.Session);

                FieldInfo field = type.GetField(memberName, bindingFlags);
                if (field != null)
                {
                    return(true);
                }

                PropertyInfo prop = type.GetProperty(memberName, bindingFlags);
                if (prop != null)
                {
                    MethodInfo getter = prop.CanRead ? prop.GetGetMethod(bindingFlags.HasFlag(BindingFlags.NonPublic)) : null;
                    if (getter != null)
                    {
                        return(true);
                    }
                }

                if (bindingFlags.HasFlag(BindingFlags.DeclaredOnly))
                {
                    break;
                }

                ct = ct.Base;
            }

            return(false);
        }
Exemplo n.º 6
0
        public override bool TryInvokeAuxMember(IHostInvokeContext context, string name, BindingFlags invokeFlags, object[] args, object[] bindArgs, out object result)
        {
            var type = GetSpecificTypeNoThrow();

            if (type != null)
            {
                var nestedTypes = type.GetScriptableNestedTypes(invokeFlags, context.DefaultAccess).Where(testType => testType.GetRootName() == name).ToIList();
                if (nestedTypes.Count > 0)
                {
                    var tempResult = Wrap(nestedTypes.Select(testType => testType.ApplyTypeArguments(type.GetGenericArguments())).ToArray());
                    if (invokeFlags.HasFlag(BindingFlags.InvokeMethod))
                    {
                        if (tempResult.TryInvoke(context, invokeFlags, args, bindArgs, out result))
                        {
                            return(true);
                        }

                        if (!invokeFlags.HasFlag(BindingFlags.GetField) && !invokeFlags.HasFlag(BindingFlags.GetProperty))
                        {
                            return(false);
                        }
                    }

                    result = tempResult;
                    return(true);
                }
            }

            result = null;
            return(false);
        }
Exemplo n.º 7
0
        private static MethodInfo FindMethod(Type type, string name, BindingFlags flags)
        {
            MethodInfo[] methods = Methods.Where(a => a.DeclaringType == type && a.Name == name).ToArray();

            foreach (MethodInfo method in methods)
            {
                if (flags.HasFlag(BindingFlags.NonPublic) && !method.IsPrivate)
                {
                    continue;
                }
                if (flags.HasFlag(BindingFlags.Public) && !method.IsPublic)
                {
                    continue;
                }
                if (flags.HasFlag(BindingFlags.Static) && !method.IsStatic)
                {
                    continue;
                }
                if (flags.HasFlag(BindingFlags.Instance) && method.IsStatic)
                {
                    continue;
                }

                return(method);
            }
            return(null);
        }
        static IEnumerable <PropertyInfo> ApplyBindingFlags(this IEnumerable <PropertyInfo> infos, BindingFlags flags)
        {
            bool pub  = flags.HasFlag(BindingFlags.Public);
            bool priv = flags.HasFlag(BindingFlags.NonPublic);

            if (pub && !priv)
            {
                infos = infos.Where(p => (p.GetMethod != null && p.GetMethod.IsPublic) || (p.SetMethod != null && p.SetMethod.IsPublic));
            }
            if (priv && !pub)
            {
                infos = infos.Where(p => (p.GetMethod == null || p.GetMethod.IsPrivate) && (p.SetMethod == null || p.SetMethod.IsPrivate));
            }

            bool stat = flags.HasFlag(BindingFlags.Static);
            bool inst = flags.HasFlag(BindingFlags.Instance);

            if (stat && !inst)
            {
                infos = infos.Where(p => (p.GetMethod != null && p.GetMethod.IsStatic) || (p.SetMethod != null && p.SetMethod.IsStatic));
            }
            else if (inst && !stat)
            {
                infos = infos.Where(p => (p.GetMethod != null && !p.GetMethod.IsStatic) || (p.SetMethod != null && !p.SetMethod.IsStatic));
            }

            return(infos);
        }
Exemplo n.º 9
0
        /// <inheritdoc />
        public override ConstructorInfo[] GetConstructors(BindingFlags bindingAttr)
        {
            var candidates = new List <ConstructorInfo>(this.constructors.Length);

            foreach (var constructor in this.constructors)
            {
                // Checks the visibility
                bool matchVisibility = false;
                if (bindingAttr.HasFlag(BindingFlags.Public) && constructor.IsPublic)
                {
                    matchVisibility = true;
                }
                else if (bindingAttr.HasFlag(BindingFlags.NonPublic) && !constructor.IsPublic)
                {
                    matchVisibility = true;
                }

                if (!matchVisibility)
                {
                    continue;
                }

                // Checks static/non static
                if (bindingAttr.HasFlag(BindingFlags.Static) && constructor.IsStatic)
                {
                    candidates.Add(constructor);
                }
                else if (bindingAttr.HasFlag(BindingFlags.Instance) && !constructor.IsStatic)
                {
                    candidates.Add(constructor);
                }
            }

            return(candidates.ToArray());
        }
Exemplo n.º 10
0
        public override System.Reflection.MethodInfo[] GetMethods(BindingFlags bindingAttr)
        {
            bool showPrivate = bindingAttr.HasFlag(BindingFlags.NonPublic);
            bool showPublic  = bindingAttr.HasFlag(BindingFlags.Public);

            return(_methods.Where(x => x.IsPublic && showPublic || x.IsPrivate && showPrivate).ToArray());
        }
Exemplo n.º 11
0
        private static FieldInfo FindField(Type type, string name, BindingFlags flags)
        {
            FieldInfo[] fields = Fields.Where(a => a.DeclaringType == type && a.Name == name).ToArray();

            foreach (FieldInfo field in fields)
            {
                if (flags.HasFlag(BindingFlags.NonPublic) && !field.IsPrivate)
                {
                    continue;
                }
                if (flags.HasFlag(BindingFlags.Public) && !field.IsPublic)
                {
                    continue;
                }
                if (flags.HasFlag(BindingFlags.Static) && !field.IsStatic)
                {
                    continue;
                }
                if (flags.HasFlag(BindingFlags.Instance) && field.IsStatic)
                {
                    continue;
                }

                return(field);
            }
            return(null);
        }
Exemplo n.º 12
0
        private static bool Filter(MethodInfo Info, BindingFlags Flags)
        {
            bool Return = false;

            if (Flags.HasFlag(BindingFlags.DeclaredOnly))
            {
                Return |= Info.IsFamily;
            }
            if (Flags.HasFlag(BindingFlags.Instance))
            {
                Return |= !Info.IsStatic;
            }
            if (Flags.HasFlag(BindingFlags.Static))
            {
                Return |= Info.IsStatic;
            }
            if (Flags.HasFlag(BindingFlags.Public))
            {
                Return |= Info.IsPublic;
            }
            if (Flags.HasFlag(BindingFlags.NonPublic))
            {
                Return |= !Info.IsPublic;
            }
            return(Return);
        }
Exemplo n.º 13
0
        public static MethodInfo GetMethod(this Type type, string name, BindingFlags bindingFlags)
        {
            var isPublic = !bindingFlags.HasFlag(BindingFlags.NonPublic);
            var isStatic = bindingFlags.HasFlag(BindingFlags.Static);

            return(type.GetRuntimeMethods()
                   .Where(m => m.IsPublic == isPublic && m.IsStatic == isStatic)
                   .FirstOrDefault(m => m.Name == name));
        }
Exemplo n.º 14
0
        private static bool TestAccessibility(MethodBase member, BindingFlags bindingFlags) {
            bool visibility = (member.IsPublic && bindingFlags.HasFlag(BindingFlags.Public)) ||
                              (!member.IsPublic && bindingFlags.HasFlag(BindingFlags.NonPublic));

            bool instance = (member.IsStatic && bindingFlags.HasFlag(BindingFlags.Static)) ||
                            (!member.IsStatic && bindingFlags.HasFlag(BindingFlags.Instance));

            return visibility && instance;
        }
Exemplo n.º 15
0
		public static MethodInfo [] GetMethods (this Type type, BindingFlags flags)
		{
			var methods = type.GetRuntimeMethods ();
			return methods.Where (m =>
			  ((flags.HasFlag (BindingFlags.Static) == m.IsStatic) || (flags.HasFlag (BindingFlags.Instance) == !m.IsStatic)
			  ) &&
			  (flags.HasFlag (BindingFlags.Public) == m.IsPublic)
			  ).ToArray ();
		}
Exemplo n.º 16
0
 private static bool check(MethodBase a, BindingFlags flags)
 {
     if (flags.HasFlag(BindingFlags.NonPublic) && a.IsPublic)
         return false;
     if (flags.HasFlag(BindingFlags.Public) && !a.IsPublic)
         return false;
     if (flags.HasFlag(BindingFlags.Static) && !a.IsStatic)
         return false;
     return true;
 }
Exemplo n.º 17
0
		public static ConstructorInfo [] GetConstructors (this Type type, BindingFlags flags)
		{
			var props = type.GetTypeInfo ().DeclaredConstructors;
			return props.Where (p =>
			  ((flags.HasFlag (BindingFlags.Static) == p.IsStatic) ||
			   (flags.HasFlag (BindingFlags.Instance) == !p.IsStatic)
			  ) &&
			  (flags.HasFlag (BindingFlags.Public) == p.IsPublic)
			  ).ToArray ();
		}
Exemplo n.º 18
0
        public static FieldInfo [] GetFields(this Type type, BindingFlags flags)
        {
            var fields = type.GetRuntimeFields();

            return(fields.Where(p =>
                                ((flags.HasFlag(BindingFlags.Static) == p.IsStatic) || (flags.HasFlag(BindingFlags.Instance) == !p.IsStatic)
                                ) &&
                                (flags.HasFlag(BindingFlags.Public) == p.IsPublic)
                                ).ToArray());
        }
Exemplo n.º 19
0
        private static bool TestAccessibility(MethodBase member, BindingFlags bindingFlags)
        {
            bool visibility = (member.IsPublic && bindingFlags.HasFlag(BindingFlags.Public)) ||
                              (!member.IsPublic && bindingFlags.HasFlag(BindingFlags.NonPublic));

            bool instance = (member.IsStatic && bindingFlags.HasFlag(BindingFlags.Static)) ||
                            (!member.IsStatic && bindingFlags.HasFlag(BindingFlags.Instance));

            return(visibility && instance);
        }
Exemplo n.º 20
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="flags"></param>
        /// <returns></returns>
        private string CreateQuery(BindingFlags flags)
        {
            string query = string.Empty;

            query += $"?network={flags.HasFlag(BindingFlags.NETWORK).ToString().ToLower()}";
            query += $"&device={flags.HasFlag(BindingFlags.DEVICE).ToString().ToLower()}";
            query += $"&endpoint={flags.HasFlag(BindingFlags.ENDPOINT).ToString().ToLower()}";
            query += $"&full={flags.HasFlag(BindingFlags.FULL).ToString().ToLower()}";

            return(query);
        }
        private bool MatchesBindingFlags(MemberInfo member)
        {
            if (_flags.HasFlag(BindingFlags.Static) && _flags.HasFlag(BindingFlags.Instance))
            {
                return(true);
            }

            return
                (!((IsStatic(member) && !_flags.HasFlag(BindingFlags.Static)) ||
                   (!IsStatic(member) && !_flags.HasFlag(BindingFlags.Instance))));
        }
Exemplo n.º 22
0
        private static bool AreBindingFlagsMatching(FieldInfo fieldInfo, BindingFlags bindingAttr)
        {
            var publicFlag = bindingAttr.HasFlag(BindingFlags.Public);
            var nonPublicFlag = bindingAttr.HasFlag(BindingFlags.NonPublic);

            var staticFlag = bindingAttr.HasFlag(BindingFlags.Static);
            var instanceFlag = bindingAttr.HasFlag(BindingFlags.Instance);

            return ((fieldInfo.IsPublic && publicFlag) || (!fieldInfo.IsPublic && nonPublicFlag)) &&
                   ((fieldInfo.IsStatic && staticFlag) || (!fieldInfo.IsStatic && instanceFlag));
        }
Exemplo n.º 23
0
        public static PropertyInfo [] GetProperties(this Type type, BindingFlags flags)
        {
            var props = type.GetRuntimeProperties();

            return(props.Where(p =>
                               ((flags.HasFlag(BindingFlags.Static) == (p.GetMethod != null && p.GetMethod.IsStatic)) ||
                                (flags.HasFlag(BindingFlags.Instance) == (p.GetMethod != null && !p.GetMethod.IsStatic))
                               ) &&
                               (flags.HasFlag(BindingFlags.Public) == (p.GetMethod != null && p.GetMethod.IsPublic)
                               )).ToArray());
        }
Exemplo n.º 24
0
        private static bool AreBindingFlagsMatching(FieldInfo fieldInfo, BindingFlags bindingAttr)
        {
            var publicFlag    = bindingAttr.HasFlag(BindingFlags.Public);
            var nonPublicFlag = bindingAttr.HasFlag(BindingFlags.NonPublic);

            var staticFlag   = bindingAttr.HasFlag(BindingFlags.Static);
            var instanceFlag = bindingAttr.HasFlag(BindingFlags.Instance);

            return(((fieldInfo.IsPublic && publicFlag) || (!fieldInfo.IsPublic && nonPublicFlag)) &&
                   ((fieldInfo.IsStatic && staticFlag) || (!fieldInfo.IsStatic && instanceFlag)));
        }
Exemplo n.º 25
0
        public override PropertyInfo[] GetProperties(BindingFlags bindingAttr)
        {
            bool showPrivate = bindingAttr.HasFlag(BindingFlags.NonPublic);
            bool showPublic  = bindingAttr.HasFlag(BindingFlags.Public);

            return(_properties.Where(x =>
            {
                var isPublic = (x.GetMethod?.IsPublic) == true || (x.SetMethod?.IsPublic) == true;
                return isPublic && showPublic || !isPublic && showPrivate;
            }).ToArray());
        }
Exemplo n.º 26
0
        public static EventInfo [] GetEvents(this Type type, BindingFlags flags)
        {
            var props = type.GetRuntimeEvents();

            return(props.Where(p =>
                               ((flags.HasFlag(BindingFlags.Static) == p.AddMethod.IsStatic) ||
                                (flags.HasFlag(BindingFlags.Instance) == !p.AddMethod.IsStatic)
                               ) &&
                               (flags.HasFlag(BindingFlags.Public) == p.AddMethod.IsPublic)
                               ).ToArray());
        }
Exemplo n.º 27
0
        public static MemberInfo[] GetMembers(this Type t, BindingFlags flags)
        {
#if NETFX_CORE
            if (!flags.HasFlag(BindingFlags.Instance) && !flags.HasFlag(BindingFlags.Static))
            {
                return(null);
            }
            return(t.GetMembers());
#else
            throw new NotImplementedException();
#endif
        }
Exemplo n.º 28
0
        public static MemberInfo[] GetMembers(this Type t, BindingFlags flags)
        {
            #if NETFX_CORE
            if (!flags.HasFlag(BindingFlags.Instance) && !flags.HasFlag(BindingFlags.Static)) return null;

            var ti = t.GetTypeInfo();
            var result = new List<MemberInfo>();
            result.AddRange(ti.DeclaredMembers);
            return result.ToArray();
            #else
            throw new NotImplementedException();
            #endif
        }
Exemplo n.º 29
0
		public object InvokeMember(string name, BindingFlags invokeAttr, Binder binder, object target, object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] namedParameters)
		{
			var numberOfUnnamedArgs = (args == null) ? 0 : args.Length;
			var numberOfNamedArgs = (namedParameters == null) ? 0 : namedParameters.Length;

			var isSupportedInvocation =
				((name == "CreateObject") && (target == this) && (numberOfUnnamedArgs == 1) && invokeAttr.HasFlag(BindingFlags.InvokeMethod) && (numberOfNamedArgs == 0)) ||
				((name == "Write") && (target == this) && (numberOfUnnamedArgs == 1) && invokeAttr.HasFlag(BindingFlags.InvokeMethod) && (numberOfNamedArgs == 0)) ||
				((name == "Redirect") && (target == this) && (numberOfUnnamedArgs == 1) && invokeAttr.HasFlag(BindingFlags.InvokeMethod) && (numberOfNamedArgs == 0));
			if (isSupportedInvocation)
				return _target.GetType().InvokeMember(name, invokeAttr, binder, _target, args);

			throw new MissingMemberException($"Invalid InvokeMember call ({name})");
		}
Exemplo n.º 30
0
        private static IEnumerable <Type> GetTypeChain(Type t, BindingFlags flags)
        {
            do
            {
                yield return(t);

                if (flags.HasFlag(BindingFlags.DeclaredOnly) || !flags.HasFlag(BindingFlags.FlattenHierarchy))
                {
                    break;
                }

                t = t.GetTypeInfo().BaseType;
            }while(t != null);
        }
Exemplo n.º 31
0
        /**
         * Tests the following BindingFlags:
         * Public, NonPublic, Static, Instance, DeclaredOnly.
         */
        private static bool TestBindingFlags(Type t, MethodInfo method, BindingFlags flags)
        {
#if NETFX_CORE
            var isValid = (flags.HasFlag(BindingFlags.Public) && method.IsPublic) ||
                          (flags.HasFlag(BindingFlags.NonPublic) && !method.IsPublic);
            isValid &= (flags.HasFlag(BindingFlags.Static) && method.IsStatic) || (flags.HasFlag(BindingFlags.Instance) && !method.IsStatic);
            if (flags.HasFlag(BindingFlags.DeclaredOnly))
            {
                isValid &= method.DeclaringType == t;
            }
            return(isValid);
#else
            throw new NotImplementedException();
#endif
        }
Exemplo n.º 32
0
        public static bool FindProperties(string name, Type owner, List <PropertyInfo> foundProperties, BindingFlags bindingFlags = BINDING_FLAGS, HashSet <Type> highestParents = null, bool searchParent = false)
        {
            var parentType = owner;

            while (parentType != null)
            {
                var properties = parentType.GetProperties(bindingFlags);
                foreach (var propertyInfo in properties)
                {
                    if (bindingFlags.HasFlag(BindingFlags.SetProperty) && propertyInfo.SetMethod == null)
                    {
                        continue;
                    }

                    if (propertyInfo.GetMethod == null)
                    {
                        continue;
                    }

                    if (propertyInfo.Name == name)
                    {
                        foundProperties.AddUnique(propertyInfo);
                    }
                }

                parentType = parentType.BaseType;

                if (highestParents.Contains(parentType))
                {
                    break;
                }
            }

            return(foundProperties.Count > 0);
        }
Exemplo n.º 33
0
        public static IEnumerable<PropertyInfo> GetProperties(this Type type, BindingFlags bindingFlags) {
            IList<PropertyInfo> properties = (bindingFlags.HasFlag(BindingFlags.DeclaredOnly))
                ? type.GetTypeInfo().DeclaredProperties.ToList()
                : type.GetTypeInfo().GetPropertiesRecursive();

            return properties.Where(p => TestAccessibility(p, bindingFlags));
        }
Exemplo n.º 34
0
 private static bool check(MethodBase a, BindingFlags flags)
 {
     if (flags.HasFlag(BindingFlags.NonPublic) && a.IsPublic)
     {
         return(false);
     }
     if (flags.HasFlag(BindingFlags.Public) && !a.IsPublic)
     {
         return(false);
     }
     if (flags.HasFlag(BindingFlags.Static) && !a.IsStatic)
     {
         return(false);
     }
     return(true);
 }
Exemplo n.º 35
0
        public static MethodInfo GetMethod(this Type This, string name, BindingFlags flags = default(BindingFlags))
        {
            var ti = This.GetTypeInfo();
            var ret = ti.GetDeclaredMethod(name);
            if (ret != null || !flags.HasFlag(BindingFlags.FlattenHierarchy) || ti.BaseType == null) return ret;

            return ti.BaseType.GetMethod(name, flags);
        }
Exemplo n.º 36
0
        public static MethodInfo GetMethod(this Type This, string methodName, Type[] paramTypes, BindingFlags flags = default(BindingFlags))
        {
            var ti = This.GetTypeInfo();
            var ret = ti.GetDeclaredMethods(methodName)
                .FirstOrDefault(x => {
                    return paramTypes.Zip(x.GetParameters().Select(y => y.ParameterType), (l, r) => l == r).All(y => y != false);
                });

            if (ret != null || !flags.HasFlag(BindingFlags.FlattenHierarchy) || ti.BaseType == null) return ret;
            return ti.BaseType.GetMethod(methodName, paramTypes, flags);
        }
Exemplo n.º 37
0
        protected virtual object InvokeInternal(string name, BindingFlags invokeAttr, Binder binder, object target, object[] args, System.Globalization.CultureInfo culture)
        {
            if (invokeAttr.HasFlag(BindingFlags.GetProperty))
            {
                var prop = GetProperty(name, invokeAttr);
                if (prop == null)
                {
                    throw new MissingFieldException("ScriptDrivenObject", name);
                }

                return prop.GetValue(target, null);

            }
            else if (invokeAttr.HasFlag(BindingFlags.SetProperty))
            {
                var prop = GetProperty(name, invokeAttr);
                if (prop == null)
                {
                    throw new MissingFieldException("ScriptDrivenObject", name);
                }

                prop.SetValue(target, args[0], invokeAttr, binder, null, culture);

                return null;
            }
            else if (invokeAttr.HasFlag(BindingFlags.InvokeMethod))
            {
                var method = GetMethod(name, invokeAttr);
                if (method == null)
                {
                    throw new MissingMethodException("ScriptDrivenObject", name);
                }

                return method.Invoke(target, invokeAttr, binder, args, culture);
            }
            else
            {
                throw new ArgumentException("No flags for property access or method call");
            }
        }
Exemplo n.º 38
0
        protected override object InnerInvokeMember(string name, BindingFlags invokeAttr, Binder binder, object target,
			object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] namedParameters)
        {
            object[] processedArgs = TypeMappingHelpers.MapToHostType(args);
            object result;

            if (name == SpecialMemberName.Default && invokeAttr.HasFlag(BindingFlags.CreateInstance))
            {
                if (_engineMode != JsEngineMode.Classic && processedArgs.Length > 0)
                {
                    processedArgs = processedArgs.Skip(1).ToArray();
                }

                result = Activator.CreateInstance(_type, processedArgs);
            }
            else
            {
                result = InvokeStandardMember(name, invokeAttr, binder, target,
                    processedArgs, modifiers, culture, namedParameters);
            }

            return TypeMappingHelpers.MapToScriptType(result, _engineMode);
        }
Exemplo n.º 39
0
        public static FieldInfo[] GetFields(this Type t, BindingFlags flags)
        {
            #if NETFX_CORE
            if (!flags.HasFlag(BindingFlags.Instance) && !flags.HasFlag(BindingFlags.Static)) return null;

            var ti = t.GetTypeInfo();
            var origFields = ti.DeclaredFields;
            var results = new List<FieldInfo>();
            foreach (var field in origFields)
            {
                var isValid = (flags.HasFlag(BindingFlags.Public) && field.IsPublic)
                    || (flags.HasFlag(BindingFlags.NonPublic) && !field.IsPublic);
                isValid &= (flags.HasFlag(BindingFlags.Static) && field.IsStatic) || (flags.HasFlag(BindingFlags.Instance) && !field.IsStatic);
                if (flags.HasFlag(BindingFlags.DeclaredOnly))
                    isValid &= field.DeclaringType == t;

                results.Add(field);
            }
            return results.ToArray();
            #else
            throw new NotImplementedException();
            #endif
        }
        /**
         * Tests the following BindingFlags:
         * Public, NonPublic, Static, Instance, DeclaredOnly.
         */
        private static bool TestBindingFlags(Type t, MethodInfo method, BindingFlags flags)
        {
#if NETFX_CORE
            var isValid = (flags.HasFlag(BindingFlags.Public) && method.IsPublic)
                || (flags.HasFlag(BindingFlags.NonPublic) && !method.IsPublic);
            isValid &= (flags.HasFlag(BindingFlags.Static) && method.IsStatic) || (flags.HasFlag(BindingFlags.Instance) && !method.IsStatic);
            if (flags.HasFlag(BindingFlags.DeclaredOnly))
                isValid &= method.DeclaringType == t;
            return isValid;
#else
            throw new NotImplementedException();
#endif
        }
Exemplo n.º 41
0
        public override bool TryInvoke(BindingFlags invokeFlags, object[] args, object[] bindArgs, out object result)
        {
            if (!invokeFlags.HasFlag(BindingFlags.InvokeMethod) || (args.Length < 1))
            {
                result = null;
                return false;
            }

            if (!args.All(arg => arg is HostType))
            {
                throw new ArgumentException("Invalid generic type argument");
            }

            var templates = types.Where(type => !type.IsSpecific()).ToArray();
            var typeArgs = args.Cast<HostType>().Select(hostType => hostType.GetTypeArg()).ToArray();

            var template = templates.FirstOrDefault(testTemplate => testTemplate.GetGenericParamCount() == typeArgs.Length);
            if (template == null)
            {
                throw new TypeLoadException(MiscHelpers.FormatInvariant("Could not find a matching generic type definition for '{0}'", templates[0].GetRootName()));
            }

            result = Wrap(template.MakeSpecificType(typeArgs));
            return true;
        }
Exemplo n.º 42
0
		public MethodMirror[] GetMethodsByNameFlags (string name, BindingFlags flags, bool ignoreCase) {
			if (vm.conn.Version.AtLeast (2, 6)) {
				long[] ids = vm.conn.Type_GetMethodsByNameFlags (id, name, (int)flags, ignoreCase);
				MethodMirror[] m = new MethodMirror [ids.Length];
				for (int i = 0; i < ids.Length; ++i)
					m [i] = vm.GetMethod (ids [i]);
				return m;
			} else {
				if (flags.HasFlag (BindingFlags.IgnoreCase)) {
					flags &= ~BindingFlags.IgnoreCase;
					ignoreCase = true;
				}
				
				if (flags == BindingFlags.Default)
					flags = BindingFlags.Public|BindingFlags.NonPublic|BindingFlags.Instance|BindingFlags.Static;
				
				MethodAttributes access = (MethodAttributes) 0;
				bool matchInstance = false;
				bool matchStatic = false;
				
				if (flags.HasFlag (BindingFlags.NonPublic)) {
					access |= MethodAttributes.Private;
					flags &= ~BindingFlags.NonPublic;
				}
				if (flags.HasFlag (BindingFlags.Public)) {
					access |= MethodAttributes.Public;
					flags &= ~BindingFlags.Public;
				}
				if (flags.HasFlag (BindingFlags.Instance)) {
					flags &= ~BindingFlags.Instance;
					matchInstance = true;
				}
				if (flags.HasFlag (BindingFlags.Static)) {
					flags &= ~BindingFlags.Static;
					matchStatic = true;
				}
				
				if ((int) flags != 0)
					throw new NotImplementedException ();
				
				var res = new List<MethodMirror> ();
				foreach (MethodMirror m in GetMethods ()) {
					if ((m.Attributes & access) == (MethodAttributes) 0)
						continue;
					
					if (!((matchStatic && m.IsStatic) || (matchInstance && !m.IsStatic)))
						continue;
					
					if ((!ignoreCase && m.Name == name) || (ignoreCase && m.Name.Equals (name, StringComparison.CurrentCultureIgnoreCase)))
						res.Add (m);
				}
				return res.ToArray ();
			}
		}
Exemplo n.º 43
0
        private IEnumerable<IMethod> IterateMethods(BindingFlags bindingFlags)
        {
            foreach (var method in
                this.typeDef.GetMembers()
                    .Where(m => m is MethodSymbol && !this.IsAny(((MethodSymbol)m).MethodKind, MethodKind.Constructor, MethodKind.StaticConstructor))
                    .Select(f => new MetadataMethodAdapter(f as MethodSymbol, this.GenericContext)))
            {
                yield return method;
            }

            if (bindingFlags.HasFlag(BindingFlags.FlattenHierarchy))
            {
                var baseType = this.BaseType;
                if (baseType != null)
                {
                    foreach (var method in baseType.GetMethods(bindingFlags))
                    {
                        yield return method;
                    }
                }
            }
        }
Exemplo n.º 44
0
 public static ConstructorInfo[] GetConstructors(this Type type, BindingFlags flags)
 {
     var props = type.GetTypeInfo ().DeclaredConstructors;
     return props.Where (p =>
       ((flags.HasFlag (BindingFlags.Static) == p.IsStatic) ||
        (flags.HasFlag (BindingFlags.Instance) == !p.IsStatic)
       ) &&
       (flags.HasFlag (BindingFlags.Public) == p.IsPublic)
       ).ToArray ();
 }
Exemplo n.º 45
0
 public static MethodInfo[] GetMethods(this Type type, BindingFlags flags)
 {
     var methods = type.GetRuntimeMethods ();
     return methods.Where (m =>
       ((flags.HasFlag (BindingFlags.Static) == m.IsStatic) || (flags.HasFlag (BindingFlags.Instance) == !m.IsStatic)
       ) &&
       (flags.HasFlag (BindingFlags.Public) == m.IsPublic)
       ).ToArray ();
 }
        public override bool TryInvokeAuxMember(string memberName, BindingFlags invokeFlags, object[] args, object[] bindArgs, out object result)
        {
            if (invokeFlags.HasFlag(BindingFlags.InvokeMethod))
            {
                if (memberName == "get")
                {
                    result = target.InvokeMember(name, BindingFlags.GetProperty, args, bindArgs, null);
                    return true;
                }

                if (memberName == "set")
                {
                    result = target.InvokeMember(name, BindingFlags.SetProperty, args, bindArgs, null);
                    return true;
                }
            }

            result = null;
            return false;
        }
Exemplo n.º 47
0
        public static EventInfo[] GetEvents(this Type type, BindingFlags flags)
        {
            var props = type.GetRuntimeEvents ();

            return props.Where (p =>
              ((flags.HasFlag (BindingFlags.Static) == p.AddMethod.IsStatic) ||
               (flags.HasFlag (BindingFlags.Instance) == !p.AddMethod.IsStatic)
              ) &&
              (flags.HasFlag (BindingFlags.Public) == p.AddMethod.IsPublic)
              ).ToArray ();
        }
Exemplo n.º 48
0
        public object InvokeMember(string name, BindingFlags invokeFlags, Binder binder, object target, object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] namedParameters)
        {
            if (invokeFlags.HasFlag(BindingFlags.InvokeMethod))
            {
                if (name == SpecialMemberNames.Default)
                {
                    return Invoke(args, false);
                }

                return InvokeMethod(name, args);
            }

            if (invokeFlags.HasFlag(BindingFlags.GetField))
            {
                int index;
                if (int.TryParse(name, NumberStyles.Integer, CultureInfo.InvariantCulture, out index))
                {
                    return GetProperty(index);
                }

                return GetProperty(name);
            }

            if (invokeFlags.HasFlag(BindingFlags.SetField))
            {
                if (args.Length != 1)
                {
                    throw new InvalidOperationException("Invalid argument count");
                }

                var value = args[0];

                int index;
                if (int.TryParse(name, NumberStyles.Integer, CultureInfo.InvariantCulture, out index))
                {
                    SetProperty(index, value);
                    return value;
                }

                SetProperty(name, value);
                return value;
            }

            throw new InvalidOperationException("Invalid member access mode");
        }
Exemplo n.º 49
0
            public override ConstructorInfo[] GetConstructors(BindingFlags bindingAttr)
            {
                // Do not expose non-public constructors for composition, since 
                // this is the behavior of all other frameworks (including Microsoft.Composition!)
                if (bindingAttr.HasFlag(BindingFlags.NonPublic))
                    bindingAttr &= ~BindingFlags.NonPublic;

                var ctor = base.GetConstructors(bindingAttr).OrderByDescending(c => c.GetParameters().Length).FirstOrDefault();
                if (ctor != null)
                    return new ConstructorInfo[] { new ImportingConstructorInfo(ctor) };

                return new ConstructorInfo[0];
            }
Exemplo n.º 50
0
        public static FieldInfo[] GetFields(this Type type, BindingFlags flags)
        {
            var fields = type.GetRuntimeFields ();

            return fields.Where (p =>
              ((flags.HasFlag (BindingFlags.Static) == p.IsStatic) || (flags.HasFlag (BindingFlags.Instance) == !p.IsStatic)
              ) &&
              (flags.HasFlag (BindingFlags.Public) == p.IsPublic)
              ).ToArray ();
        }
        private static bool IsConformWithBindingFlags(FieldInfo method, BindingFlags bindingFlags)
        {
            if (method.IsPublic && !bindingFlags.HasFlag(BindingFlags.Public))
                return false;
            if (method.IsPrivate && !bindingFlags.HasFlag(BindingFlags.NonPublic))
                return false;
            if (method.IsStatic && !bindingFlags.HasFlag(BindingFlags.Static))
                return false;
            if (!method.IsStatic && !bindingFlags.HasFlag(BindingFlags.Instance))
                return false;

            return true;
        }
 public override bool TryInvoke(BindingFlags invokeFlags, object[] args, object[] bindArgs, out object result)
 {
     result = target.InvokeMember(name, invokeFlags.HasFlag(BindingFlags.SetField) ? BindingFlags.SetProperty : BindingFlags.GetProperty, args, bindArgs, null);
     return true;
 }
Exemplo n.º 53
0
        public static IEnumerable<FieldInfo> GetFields(this Type type, BindingFlags bindingFlags)
        {
            IList<FieldInfo> fields = (bindingFlags.HasFlag(BindingFlags.DeclaredOnly))
                ? type.GetTypeInfo().DeclaredFields.ToList()
                : type.GetTypeInfo().GetFieldsRecursive();

            return fields.Where(f => TestAccessibility(f, bindingFlags)).ToList();
        }
        /**
         * This implementation currently ignores BindingFlags.FlattenHierarchy.
         */
        public static MethodInfo[] GetMethods(this Type t, BindingFlags flags = BindingFlags.Public | BindingFlags.Instance)
        {
#if NETFX_CORE
            if (!flags.HasFlag(BindingFlags.Instance) && !flags.HasFlag(BindingFlags.Static)) return null;

            var allMethods = t.GetMethods();
            var resultList = new List<MethodInfo>();
            foreach (var method in allMethods)
            {
                if (TestBindingFlags(t, method, flags))
                    resultList.Add(method);
            }
            return resultList.ToArray();
#else
            throw new NotImplementedException();
#endif
        }
        /**
         * Attention: This implementation does not throw AmbiguousMatchException when more than one
         * property is found with the specified name and matching the specified binding constraints.
         *
         * This implementation currently ignores BindingFlags.FlattenHierarchy.
         */
        public static PropertyInfo GetProperty(this Type type, string name, BindingFlags bindingAttr)
        {
#if NETFX_CORE
            var props = type.GetProperties(bindingAttr);
            if (!props.Any()) return null;
            return props.FirstOrDefault(f => bindingAttr.HasFlag(BindingFlags.IgnoreCase) ?
                f.Name.ToLower() == name.ToLower() : f.Name == name);
#else
            throw new NotImplementedException();
#endif
        }
Exemplo n.º 56
0
        public static MethodInfo[] GetMethods(this Type t, BindingFlags flags)
        {
            #if NETFX_CORE
            if (!flags.HasFlag(BindingFlags.Instance) && !flags.HasFlag(BindingFlags.Static)) return null;

            var ti = t.GetTypeInfo();
            var allMethods = ti.DeclaredMethods;
            var resultList = new List<MethodInfo>();
            foreach (var method in allMethods)
            {
                var isValid = (flags.HasFlag(BindingFlags.Public) && method.IsPublic)
                    || (flags.HasFlag(BindingFlags.NonPublic) && !method.IsPublic);
                isValid &= (flags.HasFlag(BindingFlags.Static) && method.IsStatic) || (flags.HasFlag(BindingFlags.Instance) && !method.IsStatic);
                if (flags.HasFlag(BindingFlags.DeclaredOnly))
                    isValid &= method.DeclaringType == t;

                if (isValid)
                    resultList.Add(method);
            }
            return resultList.ToArray();
            #else
            throw new NotImplementedException();
            #endif
        }
Exemplo n.º 57
0
        public static bool TryInvokeObject(object target, BindingFlags invokeFlags, object[] args, object[] bindArgs, bool tryDynamic, out object result)
        {
            var hostTarget = target as HostTarget;
            if (hostTarget != null)
            {
                if (hostTarget.TryInvoke(invokeFlags, args, bindArgs, out result))
                {
                    return true;
                }

                if (hostTarget is HostType)
                {
                    return false;
                }

                target = hostTarget.InvokeTarget;
                tryDynamic = tryDynamic && typeof(IDynamicMetaObjectProvider).IsAssignableFrom(hostTarget.Type);
            }

            if ((target != null) && invokeFlags.HasFlag(BindingFlags.InvokeMethod))
            {
                if (target is ScriptItem)
                {
                    target = DelegateFactory.CreateFunc<object>(null, target, args.Length);
                }

                var del = target as Delegate;
                if (del != null)
                {
                    result = InvokeDelegate(del, args);
                    return true;
                }

                if (tryDynamic)
                {
                    var dynamicMetaObjectProvider = target as IDynamicMetaObjectProvider;
                    if (dynamicMetaObjectProvider != null)
                    {
                        if (dynamicMetaObjectProvider.GetMetaObject(Expression.Constant(target)).TryInvoke(args, out result))
                        {
                            return true;
                        }
                    }
                }
            }

            result = null;
            return false;
        }
Exemplo n.º 58
0
        public static PropertyInfo[] GetProperties(this Type type, BindingFlags flags)
        {
            var props = type.GetRuntimeProperties ();

            return props.Where (p =>
              ((flags.HasFlag (BindingFlags.Static) == (p.GetMethod != null && p.GetMethod.IsStatic)) ||
                (flags.HasFlag (BindingFlags.Instance) == (p.GetMethod != null && !p.GetMethod.IsStatic))
              ) &&
              (flags.HasFlag (BindingFlags.Public) == (p.GetMethod != null && p.GetMethod.IsPublic)
              )).ToArray ();
        }
        public static MemberInfo[] GetMembers(this Type t, BindingFlags flags)
        {
#if NETFX_CORE
            if (!flags.HasFlag(BindingFlags.Instance) && !flags.HasFlag(BindingFlags.Static)) return null;
            return t.GetMembers();
#else
            throw new NotImplementedException();
#endif
        }
Exemplo n.º 60
0
        public override bool TryInvokeAuxMember(string name, BindingFlags invokeFlags, object[] args, object[] bindArgs, out object result)
        {
            var type = GetSpecificTypeNoThrow();
            if (type != null)
            {
                var nestedTypes = type.GetNestedTypes(invokeFlags).Where(testType => testType.GetRootName() == name).ToIList();
                if (nestedTypes.Count > 0)
                {
                    var tempResult = Wrap(nestedTypes.Select(testType => testType.ApplyTypeArguments(type.GetGenericArguments())).ToArray());
                    if (invokeFlags.HasFlag(BindingFlags.InvokeMethod))
                    {
                        return tempResult.TryInvoke(invokeFlags, args, bindArgs, out result);
                    }

                    result = tempResult;
                    return true;
                }
            }

            result = null;
            return false;
        }