An open API service indexing awesome lists of open source software.

https://github.com/li-xiaoyao/simplestring

通过XML注释或指定特性转换成字符串输出。
https://github.com/li-xiaoyao/simplestring

csharp dotnet dotnetcore netcore

Last synced: 2 months ago
JSON representation

通过XML注释或指定特性转换成字符串输出。

Awesome Lists containing this project

README

          

# SimpleString
通过XML注释或指定特性转换成字符串输出。

- [安装](#安装)
- [配置](#配置)
- [特性](#特性)
- [使用](#使用)
- [示例](#示例)

---

#### 安装
> `Install-Package SimpleString`

#### 配置

> 基础配置

````c#
var config = new Config
{
AttributeType = typeof(DescriptionAttribute), // 解析的特性,默认DescriptionAttribute(HandleType.Attribute时生效)
Name = nameof(DescriptionAttribute.Description), // 解析的特性属性,默认DescriptionAttribute.Description(HandleType.Attribute时生效)
HandleOptions = HandleOptions.Attribute, // 处理方式 Attribute|XML(默认)
HandCustomType = true, // 自定义类型处理,默认调用 ToString() 否则 ToSimpleString().
IgnoreLoopReference = true, // 忽略循环引用(存在相同引用只输出一次否则为空),默认false.
Operator = "= ", // 间隔符号,默认“ = ”
}.AddXml("xxx.xml"); // XML文档路径,类必须添加注释否则不会解析
````

> 自定义配置

````c#
// 自定义配置只需要继承基础配置也可设置默认值
public class ENConfig : Config
{
public ENConfig()
{
AttributeType = typeof(ENAttribute);
Name = nameof(ENAttribute.ENName);
IgnoreLoopReference = true;
}
}

// 自定义特性
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property)]
public class ENAttribute : Attribute
{
public ENAttribute(string eNName)
{
ENName = eNName;
}

public string ENName { get; set; }
}
````

#### 特性

> 忽略:`[IgnoreSimpleString]`

#### 使用

> 通用

```c#
// 初始化,默认全局
SimpleString.Config(c =>
{
// 默认使用XML解析 需要指定XML文档
c.AddXml(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "xxx.xml"));
});

// 直接调用
var str = new Test().ToSimpleString();

// 重写类ToString()方法(推荐)
var str = new Test().ToString();

// 创建实例调用
var str = new SimpleString(new Config
{
HandleOptions = HandleOptions.Attribute,
Operator = ": "
}).ToSimpleString(test);
```

> 注入使用

````c#
// 默认静态全局注入
services.AddSimpleString(c =>
{
// 默认XML 需要指定XML文档
c.AddXml(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "xxx.xml"));
});
// 指定特性配置注入
services.AddSimpleString();
// 指定特性配置注入
services.AddSimpleString(c =>
{
c.Operator = ": ";
});

// 构造函数注入调用
private readonly SimpleString _simpleString;
private readonly SimpleString _enSimpleString;

public HomeController(ILogger logger, SimpleString simpleString, SimpleString enSimpleString)
{
this._logger = logger;
this._simpleString = simpleString;
this._enSimpleString = enSimpleString;
}

var str = _simpleString.ToSimpleString(test);
var str = _enSimpleString.ToSimpleString(test);

````

#### 示例

````c#
///
/// 测试 (XML解析时类注释必须添加)
///
public class Test
{
///
/// XML属性1
///
[EN("MyProperty1")]
[Description("属性1")]
public int MyProperty1 { get; set; }

///
/// XML属性2
///
[EN("MyProperty2")]
[Description("属性2")]
public string MyProperty2 { get; set; }

///
/// XML属性3
///
[IgnoreSimpleString]
[EN("MyProperty3")]
[Description("属性3")]
public string MyProperty3 { get; set; }

///
/// XML属性4
///
[EN("MyProperty4")]
[Description("属性4")]
public string MyProperty4 { get; set; }

///
/// XML属性5
///
[EN("MyProperty5")]
[Description("属性5")]
public string MyProperty5 { get; set; }

///
/// XML属性6
///
[EN("MyProperty6")]
[Description("属性6")]
public string MyProperty6 { get; set; }

///
/// XML属性7
///
[EN("MyProperty7")]
[Description("属性7")]
public string MyProperty7 { get; set; }

///
/// 重写ToString 调用默认全局转换
///
///
public override string ToString() => this.ToSimpleString();
}

var test = new Test
{
MyProperty1 = 1,
MyProperty2 = "2",
MyProperty3 = "33",
MyProperty4 = "444",
MyProperty5 = "5555",
MyProperty6 = "66666"
};

var str1 = test.ToString();
var str2 = test.ToSimpleString();
var str3 = _simpleString.ToSimpleString(test);
var str4 = _enSimpleString.ToSimpleString(test);
var str5 = new SimpleString(new Config
{
HandleOptions = HandleOptions.Attribute,
Operator = ": "
}).ToSimpleString(test);

// 输出
str1:[XML属性1 = 1, XML属性2 = 2, XML属性4 = 444, XML属性5 = 5555, XML属性6 = 66666, XML属性7 = ]
str2:[XML属性1 = 1, XML属性2 = 2, XML属性4 = 444, XML属性5 = 5555, XML属性6 = 66666, XML属性7 = ]
str3:[属性1 = 1, 属性2 = 2, 属性4 = 444, 属性5 = 5555, 属性6 = 66666, 属性7 = ]
str4:[MyProperty1: 1, MyProperty2: 2, MyProperty4: 444, MyProperty5: 5555, MyProperty6: 66666, MyProperty7: ]
str5:[属性1: 1, 属性2: 2, 属性4: 444, 属性5: 5555, 属性6: 66666, 属性7: ]
````