コード例 #1
0
        private static void SetAssamblyClassType(this IServiceCollection services, Type type)
        {
            if (Attribute.GetCustomAttribute(type, typeof(CodePlugAttribute)) is CodePlugAttribute code)
            {
                var plug = new PlugInModel
                {
                    Name        = code.CodePlugName,
                    Key         = code.CodePlugName,
                    Author      = code.Author,
                    CreateDate  = code.CreateDate,
                    Description = code.Description,
                };
                if (code.Tags != null && code.Tags.Count() > 0)
                {
                    plug.Tags = code.Tags.ToList();
                }

                if (type.IsEnum)
                {
                    EnumCodeTable ect = new EnumCodeTable(type)
                    {
                        CodePlugName = code.CodePlugName//标识唯一性
                    };
                    plug.InstanceType = type;
                    plug.BaseType     = typeof(CodeTable <CodeDataModel>);
                    services.AddTransient <CodeTable <CodeDataModel> >(p =>
                    {
                        return(ect);
                    });
                }
                else
                {
                    try
                    {
                        plug.InstanceType = type;
                        plug.BaseType     = code.BaseClass;
                        services.AddTransient(code.BaseClass, (p) => {
                            object obj = Activator.CreateInstance(type, p);
                            type.GetProperty("CodePlugName").SetValue(obj, code.CodePlugName); //标识唯一性
                            return(obj);
                        });
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("注册插件失败:{0}:{1} ", ex.Message, type.Name);
                    }
                }
                Log(plug);
            }
        }
コード例 #2
0
        private static void Log(PlugInModel plug)
        {
            string log = $"注册名:{plug.Name}\r\n基类:{plug.InstanceType.ToString()}\r\n实例类:{ plug.BaseType.ToString()}\r\n路径:{3}\r\n作者:{ plug.Author}\r\n描述:{ plug.Description}";

            Console.WriteLine(log);
        }