Esempio n. 1
0
        /// <summary>通过字典参数调用</summary>
        /// <param name="obj"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public Object InvokeByDictionaryParameter(Object obj, IDictionary parameters)
        {
            //// 没有传入参数
            //if (parameters == null || parameters.Count < 1) return Invoke(obj, null);
            //! 注意:有可能没有传入参数,但是该方法是需要参数的,这个时候采用全部null的方法

            // 该方法没有参数,无视外部传入参数
            var pis = Method.GetParameters();

            if (pis == null || pis.Length < 1)
            {
                return(Invoke(obj, null));
            }

            Object[] ps = new Object[pis.Length];
            for (int i = 0; i < pis.Length; i++)
            {
                Object v = null;
                if (parameters != null && parameters.Contains(pis[i].Name))
                {
                    v = parameters[pis[i].Name];
                }
                ps[i] = TypeX.ChangeType(v, pis[i].ParameterType);
            }

            return(Handler.Invoke(obj, ps));
        }
Esempio n. 2
0
        public override void SetValue(Object obj, Object value)
        {
            if (SetHandler == null)
            {
                throw new InvalidOperationException("不支持SetValue操作!");
            }

            // 如果类型不匹配,先做类型转换
            if (value != null && !Type.IsAssignableFrom(value.GetType()))
            {
                value = TypeX.ChangeType(value, Type);
            }

            SetHandler.Invoke(obj, value);
        }
Esempio n. 3
0
        /// <summary>创建</summary>
        /// <param name="type"></param>
        /// <param name="name"></param>
        /// <param name="paramTypes">参数类型</param>
        /// <returns></returns>
        public static MethodInfoX Create(Type type, String name, Type[] paramTypes)
        {
            //var method = type.GetMethod(name, paramTypes);
            //if (method == null) method = type.GetMethod(name, DefaultBinding, null, paramTypes, null);
            //if (method == null) method = type.GetMethod(name, DefaultBinding | BindingFlags.IgnoreCase, null, paramTypes, null);
            //if (method == null && type.BaseType != typeof(Object)) return Create(type.BaseType, name, paramTypes);
            var method = TypeX.GetMethod(type, name, paramTypes);

            if (method == null)
            {
                return(null);
            }

            return(Create(method));
        }
Esempio n. 4
0
        /// <summary>快速调用静态方法</summary>
        /// <typeparam name="TTarget">目标类型</typeparam>
        /// <typeparam name="TResult">返回类型</typeparam>
        /// <param name="name"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public static TResult Invoke <TTarget, TResult>(String name, params Object[] parameters)
        {
            if (String.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("name");
            }

            MethodInfoX mix = Create(typeof(TTarget), name, TypeX.GetTypeArray(parameters));

            if (mix == null)
            {
                throw new Exception(string.Format("类{0}中无法找到{1}方法!", typeof(TTarget).Name, name));
            }

            return((TResult)mix.Invoke(null, parameters));
        }
Esempio n. 5
0
        String GetName(Boolean isfull, Boolean includeDefType = true)
        {
            var method = _Method;

            var    sb   = new StringBuilder();
            String name = null;

            if (includeDefType)
            {
                var type = method.DeclaringType ?? method.ReflectedType;
                if (type != null)
                {
                    var tx = TypeX.Create(type);
                    name = isfull ? tx.FullName : tx.Name;
                }
                else
                {
                    name = "";
                }
            }
            sb.AppendFormat("{0}.", name);
            sb.Append(method.Name);
            sb.Append("(");
            var ps = method.GetParameters();

            for (int i = 0; i < ps.Length; i++)
            {
                if (i > 0)
                {
                    sb.Append(",");
                }

                if (ps[i].ParameterType != null)
                {
                    var tx = TypeX.Create(ps[i].ParameterType);
                    name = isfull ? tx.FullName : tx.Name;
                }
                else
                {
                    name = "";
                }
                sb.AppendFormat("{0} {1}", name, ps[i].Name);
            }
            sb.Append(")");
            return(sb.ToString());
        }
Esempio n. 6
0
File: TypeX.cs Progetto: lichange/NS
        String GetName(Boolean isfull)
        {
            Type type = Type;

            if (type.IsNested)
            {
                var tx = TypeX.Create(type.DeclaringType);
                return((isfull ? tx.FullName : tx.Name) + "." + type.Name);
            }
            else if (type.IsGenericType)
            {
                var sb      = new StringBuilder();
                var typeDef = type.GetGenericTypeDefinition();
                var name    = isfull ? typeDef.FullName : typeDef.Name;
                var p       = name.IndexOf("`");
                if (p >= 0)
                {
                    sb.Append(name.Substring(0, p));
                }
                else
                {
                    sb.Append(name);
                }
                sb.Append("<");
                var ts = type.GetGenericArguments();
                for (int i = 0; i < ts.Length; i++)
                {
                    if (i > 0)
                    {
                        sb.Append(",");
                    }
                    if (!ts[i].IsGenericParameter)
                    {
                        var tx = TypeX.Create(ts[i]);
                        sb.Append(isfull ? tx.FullName : tx.Name);
                    }
                }
                sb.Append(">");
                return(sb.ToString());
            }
            else
            {
                return(isfull ? type.FullName : type.Name);
            }
        }
Esempio n. 7
0
        public override Object Invoke(Object obj, params Object[] parameters)
        {
            if (parameters == null || parameters.Length <= 0)
            {
                return(Handler.Invoke(obj, null));
            }
            else
            {
                // 预处理参数类型
                var pis = Parameters;
                for (int i = 0; i < parameters.Length && i < pis.Length; i++)
                {
                    parameters[i] = TypeX.ChangeType(parameters[i], pis[i].ParameterType);
                }

                return(Handler.Invoke(obj, parameters));
            }
        }
Esempio n. 8
0
        /// <summary>创建快速访问成员</summary>
        /// <param name="member"></param>
        /// <returns></returns>
        public static MemberInfoX Create(MemberInfo member)
        {
            if (member == null)
            {
                return(null);
            }

            switch (member.MemberType)
            {
            case MemberTypes.All:
                break;

            case MemberTypes.Constructor:
                return(ConstructorInfoX.Create(member as ConstructorInfo));

            case MemberTypes.Custom:
                break;

            case MemberTypes.Event:
                return(EventInfoX.Create(member as EventInfo));

            case MemberTypes.Field:
                return(FieldInfoX.Create(member as FieldInfo));

            case MemberTypes.Method:
                return(MethodInfoX.Create(member as MethodInfo));

            case MemberTypes.Property:
                return(PropertyInfoX.Create(member as PropertyInfo));

            case MemberTypes.TypeInfo:
            case MemberTypes.NestedType:
                return(TypeX.Create(member as Type));

            default:
                break;
            }
            return(null);
        }
Esempio n. 9
0
        public override void SetValue(Object obj, Object value)
        {
            // 如果类型不匹配,先做类型转换
            if (value != null && !Type.IsAssignableFrom(value.GetType()))
            {
                value = TypeX.ChangeType(value, Type);
            }

            // SetHandler不支持值类型
            if (Field.DeclaringType.IsValueType)
            {
                // 不相等才赋值
                Object v = GetValue(obj);
                if (!Object.Equals(value, v))
                {
                    Field.SetValue(obj, value);
                }
            }
            else
            {
                SetHandler.Invoke(obj, value);
            }
        }
Esempio n. 10
0
File: TypeX.cs Progetto: lichange/NS
        public override Object CreateInstance(params Object[] parameters)
        {
            if (Type.ContainsGenericParameters || Type.IsGenericTypeDefinition)
            {
                throw new Exception(Type.FullName + "类是泛型定义类,缺少泛型参数!");
            }

            if (Type.IsValueType || Type.IsArray)
            {
                return(Handler.Invoke(parameters));
            }

            // 准备参数类型数组,以匹配构造函数
            //var paramTypes = Type.EmptyTypes;
            //if (parameters != null && parameters.Length > 0)
            //{
            //    var list = new List<Type>();
            //    foreach (var item in parameters)
            //    {
            //        if (item != null)
            //            list.Add(item.GetType());
            //        else
            //            list.Add(typeof(Object));
            //    }
            //    paramTypes = list.ToArray();
            //}
            var paramTypes = TypeX.GetTypeArray(parameters);
            var ctor       = GetConstructor(paramTypes);
            var handler    = GetHandler(ctor);

            if (handler != null)
            {
                return(handler.Invoke(parameters));
            }
            if (paramTypes != Type.EmptyTypes)
            {
                paramTypes = Type.EmptyTypes;
                ctor       = GetConstructor(paramTypes);
                handler    = GetHandler(ctor);
                if (handler != null)
                {
                    // 更换了构造函数,要重新构造构造函数的参数
                    var ps = ctor.GetParameters();
                    parameters = new Object[ps.Length];
                    for (int i = 0; i < ps.Length; i++)
                    {
                        // 处理值类型
                        if (ps[i].ParameterType.IsValueType)
                        {
                            parameters[i] = TypeX.CreateInstance(ps[i].ParameterType);
                        }
                        else
                        {
                            parameters[i] = null;
                        }
                    }

                    // 如果这里创建失败,后面还可以创建一个未初始化
                    try
                    {
                        return(handler.Invoke(parameters));
                    }
                    catch { }
                }
            }

            // 如果没有找到构造函数,则创建一个未初始化的对象
            return(FormatterServices.GetSafeUninitializedObject(Type));
        }