예제 #1
0
        /// <summary>
        /// 创建HttpApiClient代理类
        /// 并实现指定的接口
        /// </summary>
        /// <param name="interfaceType">接口类型</param>
        /// <param name="interceptor">拦截器</param>
        /// <exception cref="ArgumentException"></exception>
        /// <exception cref="NotSupportedException"></exception>
        /// <returns></returns>
        public static object CreateProxyWithInterface(Type interfaceType, IApiInterceptor interceptor)
        {
            var apiMethods    = interfaceType.GetAllApiMethods();
            var proxyTypeCtor = proxyTypeCtorCache.GetOrAdd(
                interfaceType,
                @interface => @interface.ImplementAsHttpApiClient(apiMethods));

            return(proxyTypeCtor.Invoke(new object[] { interceptor, apiMethods }));
        }
예제 #2
0
        /// <summary>
        /// 返回类型的默认值
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static object DefaultValue(this Type type)
        {
            if (type == null)
            {
                return(null);
            }

            return(typeDefaultValueCache.GetOrAdd(type, t =>
            {
                var value = Expression.Convert(Expression.Default(t), typeof(object));
                return Expression.Lambda <Func <object> >(value).Compile().Invoke();
            }));
        }
예제 #3
0
        /// <summary>
        /// 获取类型的属性
        /// </summary>
        /// <param name="type">类型</param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException"></exception>
        public static Property[] GetProperties(Type type)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            return(cache.GetOrAdd(type, t
                                  => t
                                  .GetProperties()
                                  .Where(item => item.CanRead && item.CanWrite)
                                  .Select(item => new Property(item))
                                  .ToArray()));
        }
예제 #4
0
        /// <summary>
        /// 返回是否需要进行属性验证
        /// </summary>
        /// <param name="instance">实例</param>
        /// <returns></returns>
        private static bool IsNeedValidateProperty(object instance)
        {
            if (instance == null)
            {
                return(false);
            }

            var type = instance.GetType();

            if (type == typeof(string) || type.GetTypeInfo().IsValueType == true)
            {
                return(false);
            }

            return(cache.GetOrAdd(type, t => t.GetProperties().Any(p => p.CanRead && p.IsDefined(typeof(ValidationAttribute), true))));
        }
        /// <summary>
        /// 返回HttpApiClient代理类的实例
        /// </summary>
        /// <param name="interfaceType">接口类型</param>
        /// <param name="interceptor">拦截器</param>
        /// <exception cref="ArgumentException"></exception>
        /// <exception cref="NotSupportedException"></exception>
        /// <returns></returns>
        public static object CreateInstance(Type interfaceType, IApiInterceptor interceptor)
        {
            // 接口的实现在动态程序集里,所以接口必须为public修饰才可以创建代理类并实现此接口
            if (interfaceType.GetTypeInfo().IsVisible == false)
            {
                var message = $"WebApiClient.JIT不支持非public接口定义:{interfaceType}";
                throw new NotSupportedException(message);
            }

            var apiMethods    = interfaceType.GetAllApiMethods();
            var proxyTypeCtor = proxyTypeCtorCache.GetOrAdd(
                interfaceType,
                @interface => @interface.ImplementAsHttpApiClient(apiMethods));

            return(proxyTypeCtor.Invoke(new object[] { interceptor, apiMethods }));
        }
예제 #6
0
        /// <summary>
        /// 搜索接口的代理类型并实例化
        /// </summary>
        /// <param name="interfaceType">接口类型</param>
        /// <param name="interceptor">拦截器</param>
        /// <exception cref="TypeLoadException"></exception>
        /// <returns></returns>
        public static HttpApi CreateInstance(Type interfaceType, IApiInterceptor interceptor)
        {
            var httpApiProxy = interfaceProxyCache.GetOrAdd(interfaceType, @interface =>
            {
                var proxyType = FindProxyType(@interface);
                if (proxyType == null)
                {
                    var assemblyName = typeof(HttpApiProxy).GetTypeInfo().Assembly.GetName();
                    throw new TypeLoadException($"找不到接口{interfaceType}的代理类,请为接口所在项目重新使用Nuget安装{assemblyName.Name} {assemblyName.Version}");
                }

                var apiMethods = @interface.GetAllApiMethods();
                return(new HttpApiProxy(proxyType, apiMethods));
            });

            return(httpApiProxy.CreateInstance(interceptor));
        }
예제 #7
0
        /// <summary>
        /// 返回HttpApi代理类的实例
        /// </summary>
        /// <param name="interfaceType">接口类型</param>
        /// <param name="interceptor">拦截器</param>
        /// <exception cref="ArgumentException"></exception>
        /// <exception cref="NotSupportedException"></exception>
        /// <returns></returns>
        public static HttpApi CreateInstance(Type interfaceType, IApiInterceptor interceptor)
        {
            // 接口的实现在动态程序集里,所以接口必须为public修饰才可以创建代理类并实现此接口
            if (interfaceType.GetTypeInfo().IsVisible == false)
            {
                var message = $"WebApiClient.JIT不支持非public接口定义:{interfaceType}";
                throw new NotSupportedException(message);
            }

            var proxy = interfaceProxyCache.GetOrAdd(interfaceType, @interface =>
            {
                var apiMethods = @interface.GetAllApiMethods();
                var proxyType  = BuildProxyType(@interface, apiMethods);
                return(new HttpApiProxy(proxyType, apiMethods));
            });

            return(proxy.CreateInstance(interceptor));
        }
예제 #8
0
        /// <summary>
        /// 搜索接口的代理类型并实例化
        /// </summary>
        /// <param name="interfaceType">接口类型</param>
        /// <param name="interceptor">拦截器</param>
        /// <exception cref="TypeLoadException"></exception>
        /// <returns></returns>
        public static object CreateInstance(Type interfaceType, IApiInterceptor interceptor)
        {
            var proxyTypeCtor = proxyTypeCtorCache.GetOrAdd(interfaceType, type =>
            {
                var proxyType = FindProxyType(type);
                return(proxyType?.GetConstructor(proxyTypeCtorArgTypes));
            });

            if (proxyTypeCtor == null)
            {
                var assemblyName = typeof(HttpApiClientProxy).GetTypeInfo().Assembly.GetName();
                throw new TypeLoadException($"找不到接口{interfaceType}的代理类,请为接口所在项目重新使用Nuget安装{assemblyName.Name} {assemblyName.Version}");
            }

            var apiMethods = interfaceType.GetAllApiMethods();

            return(proxyTypeCtor.Invoke(new object[] { interceptor, apiMethods }));
        }
예제 #9
0
 /// <summary>
 /// 关联的AttributeUsageAttribute是否AllowMultiple
 /// </summary>
 /// <param name="type"></param>
 /// <returns></returns>
 public static bool IsAllowMultiple(this Type type)
 {
     return(typeAllowMultipleCache.GetOrAdd(type, (t => t.IsInheritFrom <Attribute>() && t.GetTypeInfo().GetCustomAttribute <AttributeUsageAttribute>(true).AllowMultiple)));
 }
예제 #10
0
 /// <summary>
 /// 获取接口类型及其继承的接口的所有方法
 /// 忽略HttpApiClient类型的所有接口的方法
 /// </summary>
 /// <param name="interfaceType">接口类型</param>
 /// <exception cref="ArgumentException"></exception>
 /// <exception cref="NotSupportedException"></exception>
 /// <returns></returns>
 public static MethodInfo[] GetAllApiMethods(this Type interfaceType)
 {
     return(interfaceMethodsCache.GetOrAdd(
                interfaceType,
                type => type.GetAllApiMethodsNoCache()));
 }