Beispiel #1
0
        static IGetter CreateGetter(Type[] args)
        {
            switch (args.Length)
            {
            case 0: return(ReflectionAdapter.CreateInstance(typeof(GetterCore), true) as IGetter);

            case 1: return(ReflectionAdapter.CreateInstance(typeof(GetterCore <>).MakeGenericType(args), true) as IGetter);

            case 2: return(ReflectionAdapter.CreateInstance(typeof(GetterCore <,>).MakeGenericType(args), true) as IGetter);

            case 3: return(ReflectionAdapter.CreateInstance(typeof(GetterCore <, ,>).MakeGenericType(args), true) as IGetter);

            case 4: return(ReflectionAdapter.CreateInstance(typeof(GetterCore <, , ,>).MakeGenericType(args), true) as IGetter);

            case 5: return(ReflectionAdapter.CreateInstance(typeof(GetterCore <, , , ,>).MakeGenericType(args), true) as IGetter);

            case 6: return(ReflectionAdapter.CreateInstance(typeof(GetterCore <, , , , ,>).MakeGenericType(args), true) as IGetter);

            case 7: return(ReflectionAdapter.CreateInstance(typeof(GetterCore <, , , , , ,>).MakeGenericType(args), true) as IGetter);

            case 8: return(ReflectionAdapter.CreateInstance(typeof(GetterCore <, , , , , , ,>).MakeGenericType(args), true) as IGetter);

            case 9: return(ReflectionAdapter.CreateInstance(typeof(GetterCore <, , , , , , , ,>).MakeGenericType(args), true) as IGetter);

            case 10: return(ReflectionAdapter.CreateInstance(typeof(GetterCore <, , , , , , , , ,>).MakeGenericType(args), true) as IGetter);

            case 11: return(ReflectionAdapter.CreateInstance(typeof(GetterCore <, , , , , , , , , ,>).MakeGenericType(args), true) as IGetter);

            case 12: return(ReflectionAdapter.CreateInstance(typeof(GetterCore <, , , , , , , , , , ,>).MakeGenericType(args), true) as IGetter);

            case 13: return(ReflectionAdapter.CreateInstance(typeof(GetterCore <, , , , , , , , , , , ,>).MakeGenericType(args), true) as IGetter);

            case 14: return(ReflectionAdapter.CreateInstance(typeof(GetterCore <, , , , , , , , , , , , ,>).MakeGenericType(args), true) as IGetter);

            case 15: return(ReflectionAdapter.CreateInstance(typeof(GetterCore <, , , , , , , , , , , , , ,>).MakeGenericType(args), true) as IGetter);

            case 16: return(ReflectionAdapter.CreateInstance(typeof(GetterCore <, , , , , , , , , , , , , , ,>).MakeGenericType(args), true) as IGetter);

            case 17: return(ReflectionAdapter.CreateInstance(typeof(GetterCore <, , , , , , , , , , , , , , , ,>).MakeGenericType(args), true) as IGetter);

            case 18: return(ReflectionAdapter.CreateInstance(typeof(GetterCore <, , , , , , , , , , , , , , , , ,>).MakeGenericType(args), true) as IGetter);

            case 19: return(ReflectionAdapter.CreateInstance(typeof(GetterCore <, , , , , , , , , , , , , , , , , ,>).MakeGenericType(args), true) as IGetter);

            case 20: return(ReflectionAdapter.CreateInstance(typeof(GetterCore <, , , , , , , , , , , , , , , , , , ,>).MakeGenericType(args), true) as IGetter);

            case 21: return(ReflectionAdapter.CreateInstance(typeof(GetterCore <, , , , , , , , , , , , , , , , , , , ,>).MakeGenericType(args), true) as IGetter);

            case 22: return(ReflectionAdapter.CreateInstance(typeof(GetterCore <, , , , , , , , , , , , , , , , , , , , ,>).MakeGenericType(args), true) as IGetter);

            case 23: return(ReflectionAdapter.CreateInstance(typeof(GetterCore <, , , , , , , , , , , , , , , , , , , , , ,>).MakeGenericType(args), true) as IGetter);

            case 24: return(ReflectionAdapter.CreateInstance(typeof(GetterCore <, , , , , , , , , , , , , , , , , , , , , , ,>).MakeGenericType(args), true) as IGetter);

            case 25: return(ReflectionAdapter.CreateInstance(typeof(GetterCore <, , , , , , , , , , , , , , , , , , , , , , , ,>).MakeGenericType(args), true) as IGetter);

            case 26: return(ReflectionAdapter.CreateInstance(typeof(GetterCore <, , , , , , , , , , , , , , , , , , , , , , , , ,>).MakeGenericType(args), true) as IGetter);

            case 27: return(ReflectionAdapter.CreateInstance(typeof(GetterCore <, , , , , , , , , , , , , , , , , , , , , , , , , ,>).MakeGenericType(args), true) as IGetter);

            case 28: return(ReflectionAdapter.CreateInstance(typeof(GetterCore <, , , , , , , , , , , , , , , , , , , , , , , , , , ,>).MakeGenericType(args), true) as IGetter);

            case 29: return(ReflectionAdapter.CreateInstance(typeof(GetterCore <, , , , , , , , , , , , , , , , , , , , , , , , , , , ,>).MakeGenericType(args), true) as IGetter);
            }
            throw new NotSupportedException("The maximum number of methods arguments that can be used is 30.");
        }
Beispiel #2
0
        static object GetMemberObject(MemberExpression exp)
        {
            var    names = new List <string>();
            object targt = null;
            Type   type  = null;

            //find member root object.
            var member = exp;

            while (member != null)
            {
                names.Add(member.Member.Name);

                //static method.
                if (member.Expression == null)
                {
                    type = member.Member.DeclaringType;
                    break;
                }

                var constant = member.Expression as ConstantExpression;
                if (constant != null)
                {
                    targt = constant.Value;
                    type  = constant.Type;
                    break;
                }

                var method = member.Expression as MethodCallExpression;
                if (method != null)
                {
                    type  = method.Type;
                    targt = GetMethodObject(method);
                    break;
                }

                var newExp = member.Expression as NewExpression;
                if (newExp != null)
                {
                    type  = newExp.Type;
                    targt = GetNewObject(newExp);
                    break;
                }

                member = member.Expression as MemberExpression;
            }

            //name.
            var getterName = type.FullName + "@" + string.Join("@", names.ToArray());

            //getter.
            IGetter getter;

            lock (_memberGet)
            {
                if (!_memberGet.TryGetValue(getterName, out getter))
                {
                    var        param  = Expression.Parameter(type, "param");
                    Expression target = param;
                    names.Reverse();
                    if (targt == null)
                    {
                        target = ReflectionAdapter.StaticPropertyOrField(type, names[0]);
                        names.RemoveAt(0);
                    }
                    if (names.Count == 0)
                    {
                        getter = ReflectionAdapter.CreateInstance(typeof(GetterCore), true) as IGetter;
                        getter.Init(Expression.Convert(target, typeof(object)), new ParameterExpression[0]);
                    }
                    else
                    {
                        foreach (var e in names)
                        {
                            target = Expression.PropertyOrField(target, e);
                        }
                        getter = ReflectionAdapter.CreateInstance(typeof(GetterCore <>).MakeGenericType(type), true) as IGetter;
                        getter.Init(Expression.Convert(target, typeof(object)), new[] { param });
                    }
                    _memberGet.Add(getterName, getter);
                }
            }
            return(getter.GetMemberObject(new object[] { targt }));
        }