Example #1
0
        public static IObserver CreateBindingSource(RelativeSourceInfo node, IDataContext context, [NotNull] object target, string pathEx)
        {
            if (target == null)
            {
                throw BindingExceptionManager.InvalidBindingTarget(node.Path);
            }
            string path = node.Path ?? string.Empty;

            if (!string.IsNullOrEmpty(pathEx))
            {
                path = MergePath(path, pathEx);
            }

            if (node.Type != RelativeSourceInfo.SelfType)
            {
                if (node.Type == RelativeSourceInfo.ContextSourceType)
                {
                    target = BindingServiceProvider.ContextManager.GetBindingContext(target);
                }
                else
                {
                    target = new ParentSourceValue(target, node);
                }
            }
            return(CreateBindingSource(context, path, target, false));
        }
        internal static MethodData FindIndexer(this ArgumentData target, IList <ArgumentData> args, bool staticAccess)
        {
            var type    = target.Type;
            var methods = new List <MethodInfo>();

            foreach (var property in type.GetPropertiesEx(MemberFlags.Public | MemberFlags.Instance))
            {
                if (property.GetIndexParameters().Length == args.Count)
                {
                    var m = property.GetGetMethod(true);
                    if (m != null && m.IsStatic == staticAccess)
                    {
                        methods.Add(m);
                    }
                }
            }


            var method = FindBestMethod(target, methods, args, Empty.Array <Type>());

            if (method == null)
            {
                throw BindingExceptionManager.InvalidBindingMember(type, "Item[]");
            }
            return(method);
        }
        internal static MethodData FindMethod(this ArgumentData target, string methodName, Type[] typeArgs,
                                              IList <ArgumentData> args, IEnumerable <Type> knownTypes, bool staticAccess)
        {
            var type = target.Type;
            const MemberFlags flags = MemberFlags.Public | MemberFlags.Static | MemberFlags.Instance;
            var methods             = new List <MethodInfo>();

            foreach (var info in type.GetMethodsEx(flags))
            {
                if (info.Name == methodName && info.IsStatic == staticAccess)
                {
                    methods.Add(info);
                }
            }
            if (!staticAccess)
            {
                methods.AddRange(GetExtensionsMethods(methodName, knownTypes));
            }
            var method = FindBestMethod(target, methods, args, typeArgs);

            if (method == null)
            {
                throw BindingExceptionManager.InvalidBindingMember(type, methodName);
            }
            return(method);
        }
        /// <summary>
        /// Attempts to subscribe to the event.
        /// </summary>
        public static IDisposable TrySubscribe([NotNull] this IWeakEventManager eventManager, [NotNull] object target, string eventName, IEventListener listener,
                                               IDataContext context = null)
        {
            Should.NotBeNull(eventManager, "eventManager");
            Should.NotBeNull(target, "target");
            var @event = target.GetType().GetEventEx(eventName, MemberFlags.Instance | MemberFlags.Public);

            if (@event == null)
            {
                throw BindingExceptionManager.MissingEvent(target, eventName);
            }
            return(eventManager.TrySubscribe(target, @event, listener, context));
        }
        public static string GetMemberPath([NotNull] LambdaExpression expression, string separator = ".")
        {
            var        ret     = new StringBuilder();
            Expression current = expression.Body;

            while (current.NodeType != ExpressionType.Parameter)
            {
                // This happens when a value type gets boxed
                if (current.NodeType == ExpressionType.Convert || current.NodeType == ExpressionType.ConvertChecked)
                {
                    var ue = (UnaryExpression)current;
                    current = ue.Operand;
                    continue;
                }

                string memberName;
                var    methodCallExpression = current as MethodCallExpression;
                if (methodCallExpression != null)
                {
                    if (methodCallExpression.Method.Name != "get_Item")
                    {
                        throw BindingExceptionManager.InvalidBindingMemberExpression();
                    }
                    string s = string.Join(",", methodCallExpression.Arguments.Cast <ConstantExpression>().Select(e => e.Value));
                    memberName = "[" + s + "]";
                    current    = methodCallExpression.Object;
                }
                else if (current.NodeType != ExpressionType.MemberAccess)
                {
                    throw BindingExceptionManager.InvalidBindingMemberExpression();
                }
                else
                {
                    var me = (MemberExpression)current;
                    memberName = me.Member.Name;
                    current    = me.Expression;
                }

                if (ret.Length != 0)
                {
                    ret.Insert(0, separator);
                }
                ret.Insert(0, memberName);
                if (current == null)
                {
                    break;
                }
            }
            return(ret.ToString());
        }
Example #6
0
        internal static void CheckDuplicateLambdaParameter(ICollection <string> parameters)
        {
            if (parameters.Count == 0)
            {
                return;
            }
            var strings = new HashSet <string>();

            foreach (string parameter in parameters)
            {
                if (strings.Contains(parameter))
                {
                    throw BindingExceptionManager.DuplicateLambdaParameter(parameter);
                }
                strings.Add(parameter);
            }
        }
        internal static MemberInfo FindPropertyOrField(this Type type, string memberName, bool staticAccess, bool throwOnError)
        {
            var          flags    = MemberFlags.Public | (staticAccess ? MemberFlags.Static : MemberFlags.Instance);
            PropertyInfo property = type.GetPropertyEx(memberName, flags);

            if (property != null)
            {
                return(property);
            }
            FieldInfo field = type.GetFieldEx(memberName, flags);

            if (field == null && throwOnError)
            {
                throw BindingExceptionManager.InvalidBindingMember(type, memberName);
            }
            return(field);
        }
        private static MethodData TryInferGenericMethod(MethodInfo method, IList <ArgumentData> args)
        {
            bool hasUnresolved;
            var  genericMethod = TryInferGenericMethod(method, args, out hasUnresolved);

            if (genericMethod == null)
            {
                return(null);
            }
            if (hasUnresolved)
            {
                return(new MethodData(genericMethod, list =>
                {
                    bool unresolved;
                    var m = TryInferGenericMethod(method, list, out unresolved);
                    if (unresolved)
                    {
                        throw BindingExceptionManager.InvalidBindingMember(genericMethod.DeclaringType, method.Name);
                    }
                    return m;
                }));
            }
            return(new MethodData(genericMethod));
        }
Example #9
0
        internal static bool TryGetMemberPath(Expression expression, string separator, bool throwOnError, out Expression lastExpression, out string path)
        {
            lastExpression = expression;
            path           = null;
            if (expression == null)
            {
                return(false);
            }
            var  ret   = new StringBuilder();
            bool error = false;

            while (lastExpression.NodeType != ExpressionType.Parameter)
            {
                if (error)
                {
                    if (throwOnError)
                    {
                        throw BindingExceptionManager.InvalidBindingMemberExpression();
                    }
                    path = ret.ToString();
                    return(false);
                }

                // This happens when a value type gets boxed
                if (lastExpression.NodeType == ExpressionType.Convert ||
                    lastExpression.NodeType == ExpressionType.ConvertChecked)
                {
                    var ue = (UnaryExpression)lastExpression;
                    lastExpression = ue.Operand;
                    continue;
                }

                string memberName;
                var    methodCallExpression = lastExpression as MethodCallExpression;
                if (methodCallExpression != null)
                {
                    var memberAttribute = methodCallExpression.Method
                                          .GetAttributes()
                                          .OfType <BindingSyntaxMemberAttribute>()
                                          .FirstOrDefault();

                    if (memberAttribute != null)
                    {
                        if (methodCallExpression.Arguments.Count != 1 && methodCallExpression.Arguments.Count != 2)
                        {
                            error = true;
                            continue;
                        }
                        lastExpression = methodCallExpression.Arguments[0];
                        if (methodCallExpression.Arguments.Count == 2)
                        {
                            if (!methodCallExpression.Arguments[1].TryGetStaticValue(out memberName, false))
                            {
                                error = true;
                                continue;
                            }
                        }
                        else
                        {
                            memberName = memberAttribute.GetMemberName(methodCallExpression.Method);
                        }
                    }
                    else
                    {
                        if (methodCallExpression.Method.Name != "get_Item")
                        {
                            error = true;
                            continue;
                        }
                        var builder = new StringBuilder("[");
                        var args    = methodCallExpression.Arguments;
                        for (int i = 0; i < args.Count; i++)
                        {
                            object value;
                            if (!args[i].TryGetStaticValue(out value, false))
                            {
                                error = true;
                                break;
                            }
                            if (i != 0)
                            {
                                builder.Append(",");
                            }
                            builder.Append(value.ToStringValue());
                        }
                        if (error)
                        {
                            continue;
                        }
                        builder.Append("]");
                        memberName     = builder.ToString();
                        lastExpression = methodCallExpression.Object;
                    }
                }
                else if (lastExpression.NodeType != ExpressionType.MemberAccess)
                {
                    error = true;
                    continue;
                }
                else
                {
                    var me = (MemberExpression)lastExpression;
                    memberName     = me.Member.Name;
                    lastExpression = me.Expression;
                }

                if (ret.Length != 0 && ret[0] != '[')
                {
                    ret.Insert(0, separator);
                }
                ret.Insert(0, memberName);
                if (lastExpression == null)
                {
                    break;
                }
            }
            path = ret.ToString();
            return(true);
        }
Example #10
0
 public static Exception DuplicateLambdaParameter(string parameterName)
 {
     return(BindingExceptionManager.DuplicateLambdaParameter(parameterName));
 }