private void UpdateCollection()
        {
            var context = new ServiceRegisterDescriptorProviderContext();

            for (var i = 0; i < _serviceRegisterDescriptorProviders.Length; i++)
            {
                _serviceRegisterDescriptorProviders[i].OnProvidersExecuting(context);
            }

            for (var i = _serviceRegisterDescriptorProviders.Length - 1; i >= 0; i--)
            {
                _serviceRegisterDescriptorProviders[i].OnProvidersExecuted(context);
            }

            _collection = new ServiceRegisterDescriptorCollection(
                new ReadOnlyCollection <ServiceRegisterDescriptor>(context.Results));
        }
Пример #2
0
        /// <summary>
        /// 使用反射从程序集中获取所有包含ServiceRegisterDescriptor特性的接口定义
        /// </summary>
        /// <param name="context"></param>
        public void OnProvidersExecuting(ServiceRegisterDescriptorProviderContext context)
        {
            Assembly[]         assemblys = AssemblyDiscovery.Discovery();
            IEnumerable <Type> types     = assemblys.SelectMany(m => m.GetTypes().Where(t => t.GetTypeInfo().GetCustomAttributes().Any(a => a.GetType() == typeof(ServiceRegisterDescriptorAttribute)))).ToList();

            foreach (var type in types)
            {
                TypeInfo typeInfo = type.GetTypeInfo();
                ServiceRegisterDescriptorAttribute attr = type.GetTypeInfo().GetCustomAttributes().FirstOrDefault(m => m.GetType() == typeof(ServiceRegisterDescriptorAttribute)) as ServiceRegisterDescriptorAttribute;

                if (typeInfo.IsGenericTypeDefinition && attr.GenericType == null)
                {
                    throw new NotSupportedException(nameof(attr));
                }

                Type[] impTypes = null;
                if ((typeInfo.IsInterface || typeInfo.IsAbstract) && attr.Imp == null)
                {
                    //从程序集中获取所有实现了该服务端类
                    if (typeInfo.IsGenericTypeDefinition && typeInfo.IsInterface)
                    {
                        impTypes = assemblys.SelectMany(m => m.GetTypes().Where(t => t.GetTypeInfo().IsClass&& !t.GetTypeInfo().IsAbstract&& t.GetTypeInfo().GetInterfaces().Any(i => i.GetTypeInfo().IsGenericType&& i.GetGenericTypeDefinition() == type))).ToArray();
                    }
                    else
                    {
                        if (typeInfo.IsInterface)
                        {
                            impTypes = assemblys.SelectMany(m => m.GetTypes().Where(t => t.GetTypeInfo().IsClass&& !t.GetTypeInfo().IsAbstract&& t.GetTypeInfo().GetInterfaces().Any(i => i == type))).ToArray();
                        }
                        else
                        {
                            impTypes = assemblys.SelectMany(m => m.GetTypes().Where(t => t.GetTypeInfo().IsClass&& !t.GetTypeInfo().IsAbstract&& t.GetTypeInfo().IsSubclassOf(type))).ToArray();
                        }
                    }
                }
                else if (attr.Imp != null)
                {
                    impTypes = new Type[1] {
                        attr.Imp
                    };
                }
                else
                {
                    impTypes = new Type[1] {
                        type
                    };
                }

                foreach (var imp in impTypes)
                {
                    ServiceRegisterDescriptor d = new ServiceRegisterDescriptor
                    {
                        AllowMultipleImp = attr.AllowMultipleImp,
                        Imp         = imp,
                        ServiceType = type,
                        LifeTime    = attr.LifeTime
                    };

                    if (typeInfo.IsGenericType)
                    {
                        d.GenericParameterTypes = assemblys.SelectMany(m => m.GetTypes().Where(t => !t.GetTypeInfo().IsAbstract&& (t.GetTypeInfo().IsSubclassOf(attr.GenericType) || t == attr.GenericType))).ToArray();
                    }
                    context.Results.Add(d);
                }
            }
        }
Пример #3
0
 public void OnProvidersExecuted(ServiceRegisterDescriptorProviderContext context)
 {
 }