/// <summary> /// 查找所有输出类型中的API /// </summary> /// <param name="exportedTypes">输出类型</param> /// <param name="factory">API类描述创建服务</param> private void FindAllApis(IEnumerable <Type> exportedTypes, IApiClassDescriptorFactory factory) { if (exportedTypes == null) { throw new ArgumentNullException(nameof(exportedTypes)); } if (factory == null) { factory = DefaultIApiClassDescriptorFactory.Instance; } var ns = new Dictionary <string, IList <ApiClassDescriptor> >(); foreach (var t in exportedTypes) { var apiclass = factory.Create(t); if (apiclass == null) { continue; } _types.Add(apiclass); if (ns.TryGetValue(t.Namespace, out var list) == false) { list = new List <ApiClassDescriptor>(); ns.Add(t.Namespace, list); } list.Add(apiclass); _apis.AddRange(apiclass.Apis); } _namespaces.AddRange(ns.Select(it => new NamespaceDescriptor(it.Key, it.Value))); }
/// <summary> /// 初始化容器 /// </summary> /// <param name="name">容器名称</param> /// <param name="exportedTypes">输出类型</param> /// <param name="factory">API类描述创建服务</param> public ApiContainer(string name, IEnumerable <Type> exportedTypes, IApiClassDescriptorFactory factory = null) { Name = name ?? throw new ArgumentNullException(nameof(name)); _namespaces = new List <NamespaceDescriptor>(); _types = new List <ApiClassDescriptor>(); _apis = new List <ApiDescriptor>(); var @base = typeof(ApiGlobalEvents).GetTypeInfo(); var types = exportedTypes as Type[] ?? exportedTypes.ToArray(); _events = types.Select(t => t.GetTypeInfo()) .Where(t => @base.IsAssignableFrom(t) && t.IsAbstract == false && t.ContainsGenericParameters == false) .Select(t => (ApiGlobalEvents)Activator.CreateInstance(t.AsType())) .ToList(); FindAllApis(types, factory); Namespaces = _namespaces.AsReadOnly(); ApiClasses = _types.AsReadOnly(); Apis = _apis.AsReadOnly(); foreach (var ns in Namespaces) { ns.Extends["$ApiContainer"] = this; } foreach (var apiClass in ApiClasses) { apiClass.Extends["$ApiContainer"] = this; foreach (var property in apiClass.Properties) { property.Extends["$ApiContainer"] = this; } } foreach (var api in Apis) { api.Extends["$ApiContainer"] = this; } }