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

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

                SlimTypeInfo typeInfo = _instanceCache.GetOrAdd(fullName, (v) =>
                {
                    Type[] argsTypes = null;

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

                    var mi = type.GetMethod(methodName);

                    return(new SlimTypeInfo()
                    {
                        Type = type,
                        MethodInfo = mi,
                        ArgTypes = argsTypes
                    });
                });
                typeInfo.Instance = Activator.CreateInstance(type);
                return(typeInfo);
            }
            return(null);
        }
Beispiel #2
0
        /// <summary>
        /// 添加或获取实例
        /// </summary>
        /// <param name="type"></param>
        /// <param name="info"></param>
        /// <returns></returns>
        public static SlimTypeInfo GetOrAddInstance(Type type, MethodInfo info)
        {
            lock (_syncLock)
            {
                if (type.IsInterface)
                {
                    throw new Exception("服务方法中不能包含接口内容!");
                }

                var fullName = type.FullName + info.Name;

                SlimTypeInfo typeInfo = _instanceCache.GetOrAdd(fullName, (v) =>
                {
                    return(new SlimTypeInfo()
                    {
                        Type = type,
                        MethodInfo = info
                    });
                });
                typeInfo.Instance = Activator.CreateInstance(type);
                return(typeInfo);
            }
        }