예제 #1
0
        /// <summary>
        /// 根据命令行参数打印示例。
        /// </summary>
        /// <param name="options">命令行参数</param>
        public static void Print(ExampleOptions options)
        {
            var    uploaders   = NamedInterfaceLoader.Load(typeof(IUploader));
            var    dataSources = NamedInterfaceLoader.Load(typeof(IDataSource));
            string content     = string.Empty;

            if (string.IsNullOrEmpty(options.List) == false)
            {
                if (options.List == "datasource")
                {
                    content = string.Join('\n', dataSources.Keys);
                    Console.WriteLine("已支持以下数据源:");
                }
                else if (options.List == "uploader")
                {
                    content = string.Join('\n', uploaders.Keys);
                    Console.WriteLine("已支持以下上传类:");
                }
                else
                {
                    Console.WriteLine("只能列出 datasource 或 uploader");
                }
            }
            else if (string.IsNullOrEmpty(options.Uploader) == false)
            {
                if (uploaders.ContainsKey(options.Uploader))
                {
                    content = JsonConvert.SerializeObject((Activator.CreateInstance(uploaders[options.Uploader]) as IExampled).GetExample(),
                                                          Formatting.Indented,
                                                          new NameConverter());
                }
                else
                {
                    Console.WriteLine($"不存在uploader类: { options.Uploader }");
                }
            }
            else if (string.IsNullOrEmpty(options.DataSource) == false)
            {
                if (dataSources.ContainsKey(options.DataSource))
                {
                    content = JsonConvert.SerializeObject((Activator.CreateInstance(dataSources[options.DataSource]) as IExampled).GetExample(),
                                                          Formatting.Indented,
                                                          new NameConverter());
                }
                else
                {
                    Console.WriteLine($"不存在datasource类: { options.DataSource }");
                }
            }
            else
            {
                content = GetExampleConfig(uploaders, dataSources);
            }

            if (string.IsNullOrEmpty(content) == false)
            {
                Console.WriteLine(content);
                if (string.IsNullOrEmpty(options.Path) == false)
                {
                    //保存到文件。
                    using (StreamWriter stream = new StreamWriter(options.Path, false, Encoding.UTF8))
                    {
                        stream.Write(content);
                    }

                    Console.WriteLine("\n\n");
                    Log.Info(string.Format("文件已经保存到\"{0}\"。", options.Path));
                }
            }

#if !DEBUG
            Environment.Exit(0);
#endif
        }
예제 #2
0
 /// <summary>
 /// 初始化
 /// </summary>
 public ConfigurationConverter()
 {
     _supportDataSourceTypes = NamedInterfaceLoader.Load(typeof(IDataSource));
     _supportUploaderTypes   = NamedInterfaceLoader.Load(typeof(IUploader));
 }