Exemple #1
0
        /// <summary>
        /// 添加或获取实例
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static TypeInfo GetOrAddInstance(Type type, string methodName = "Add")
        {
            lock (SyncRoot)
            {
                if (type.IsInterface)
                {
                    throw new Exception("服务方法中不能包含接口内容!");
                }

                if (type.IsClass)
                {
                    var fullName = type.FullName + methodName;

                    TypeInfo typeInfo;

                    if (!InstanceCache.TryGetValue(fullName, out typeInfo))
                    {
                        Type[] argsTypes = null;

                        if (type.IsGenericType)
                        {
                            argsTypes = type.GetGenericArguments();
                            type      = type.GetGenericTypeDefinition().MakeGenericType(argsTypes);
                        }

                        var mi = type.GetMethod(methodName);

                        typeInfo = new TypeInfo()
                        {
                            Type              = type,
                            Instance          = Activator.CreateInstance(type),
                            FastInvokeHandler = mi == null ? null : FastInvoke.GetMethodInvoker(mi),
                            ArgTypes          = argsTypes
                        };
                        InstanceCache.Add(fullName, typeInfo);
                    }
                    else
                    {
                        typeInfo.Instance = Activator.CreateInstance(type);
                    }
                    return(typeInfo);
                }
                return(null);
            }
        }
Exemple #2
0
        /// <summary>
        /// 将List~object转换成List~T
        /// </summary>
        /// <param name="params"></param>
        /// <returns></returns>
        public static object ToGList(this List <object> @params)
        {
            var type = @params.GetType();

            var stype = type.GenericTypeArguments[0];

            var gtype = type.GetGenericTypeDefinition().MakeGenericType(stype);

            var result = Activator.CreateInstance(gtype);

            var addMethod = gtype.GetMethod("AddRange");

            var methodInvoker = FastInvoke.GetMethodInvoker(addMethod);

            methodInvoker.Invoke(result, @params.ToArray());

            return(result);
        }
Exemple #3
0
        /// <summary>
        /// 属性取值
        /// </summary>
        /// <param name="type"></param>
        /// <param name="obj"></param>
        /// <param name="property"></param>
        /// <returns></returns>

        public static object Getter(Type type, Object obj, PropertyInfo property)
        {
            lock (_syncRoot)
            {
                var name = type.FullName + property.Name + "Getter";

                if (_invokeInfos.ContainsKey(name))
                {
                    return(_invokeInfos[name].Invoke(obj));
                }
                else
                {
                    var del = FastInvoke.GetMethodInvoker(property.GetGetMethod(true));
                    _invokeInfos.Add(name, del);
                    return(del.Invoke(obj));
                }
            }
        }
Exemple #4
0
        /// <summary>
        /// 属性赋值
        /// </summary>
        /// <param name="type"></param>
        /// <param name="obj"></param>
        /// <param name="property"></param>
        /// <param name="val"></param>
        public static void Setter(Type type, Object obj, PropertyInfo property, object val)
        {
            lock (_syncRoot)
            {
                var name = type.FullName + property.Name + "Setter";

                if (_invokeInfos.ContainsKey(name))
                {
                    _invokeInfos[name].Invoke(obj, val);
                }
                else
                {
                    var del = FastInvoke.GetMethodInvoker(property.GetSetMethod(true));
                    _invokeInfos.Add(name, del);
                    del.Invoke(obj, val);
                }
            }
        }