using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Common
{
public static class AutoMapHelper
{
/// <summary>
/// 类型映射,默认字段名字一一对应
/// </summary>
/// <typeparam name="TDestination"></typeparam>
/// <param name="obj"></param>
/// <returns></returns>
public static TDestination AutoMapTo<TDestination>(this object obj)
{
if (obj == null) return default(TDestination);
var config = new AutoMapper.MapperConfiguration(cfg => cfg.CreateMap(obj.GetType(), typeof(TDestination)));
return config.CreateMapper().Map<TDestination>(obj);
}
/// <summary>
/// 类型映射,可指定映射字段的配置信息
/// </summary>
/// <typeparam name="TSource">源数据:要被转化的实体对象</typeparam>
/// <typeparam name="TDestination">目标数据:转换后的实体对象</typeparam>
/// <param name="source">任何引用类型对象</param>
/// <param name="cfgExp">可为null,则自动一一映射</param>
/// <returns></returns>
public static TDestination AutoMapTo<TSource, TDestination>(this TSource source, Action<AutoMapper.IMapperConfigurationExpression> cfgExp)
where TDestination : class
where TSource : class
{
if (source == null) return default(TDestination);
var config = new AutoMapper.MapperConfiguration(cfgExp != null ? cfgExp : cfg => cfg.CreateMap<TSource, TDestination>());
var mapper = config.CreateMapper();
return mapper.Map<TDestination>(source);
}
/// <summary>
/// 类型映射,默认字段名字一一对应
/// </summary>
/// <typeparam name="TSource">源数据:要被转化的实体对象</typeparam>
/// <typeparam name="TDestination">目标数据:转换后的实体对象</typeparam>
/// <param name="source">任何引用类型对象</param>
/// <returns>转化之后的实体</returns>
public static TDestination AutoMapTo<TSource, TDestination>(this TSource source)
where TDestination : class
where TSource : class
{
if (source == null) return default(TDestination);
var config = new AutoMapper.MapperConfiguration(cfg => cfg.CreateMap<TSource, TDestination>());
var mapper = config.CreateMapper();
return mapper.Map<TDestination>(source);
}
/// <summary>
/// 集合列表类型映射,默认字段名字一一对应
/// </summary>
/// <typeparam name="TDestination">转化之后的实体对象,可以理解为viewmodel</typeparam>
/// <typeparam name="TSource">要被转化的实体对象,Entity</typeparam>
/// <param name="source">通过泛型指定的这个扩展方法的类型,理论任何引用类型</param>
/// <returns>转化之后的实体列表</returns>
public static IEnumerable<TDestination> AutoMapTo<TSource, TDestination>(this IEnumerable<TSource> source)
where TDestination : class
where TSource : class
{
if (source == null) return new List<TDestination>();
var config = new AutoMapper.MapperConfiguration(cfg => cfg.CreateMap<TSource, TDestination>());
var mapper = config.CreateMapper();
return mapper.Map<List<TDestination>>(source);
}
}
}
Js 判断是否为空
/* @desc 判断`obj`是否为空
* @param {Object} obj
* @return {Boolean}
*/
function isEmptyObject(obj) {
if (!obj || typeof obj !== 'object' || Array.isArray(obj))
return false
return !Object.keys(obj).length
}
liunx Oralce 监听模板
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(SID_NAME = PLSExtProc)
(ORACLE_HOME = /oracle/product/10.2.0/db_1)
(PROGRAM = extproc)
)
(SID_DESC =
(GLOBAL_DBNAME = orcl)
(ORACLE_HOME = /oracle/product/10.2.0/db_1)
(SID_NAME = orcl)
)
)
LISTENER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1))
(ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
)
)
Echarts常用配置
const option = {
title: {
text: "折线图",
textStyle: {
color: '#fff',
fontSize: this.fontSize(0.4)
}
},
xAxis: {
type: "category",
// boundaryGap: false,
axisLabel: {
show: true,
margin: this.fontSize(0.4),
textStyle: {
color: '#fff', //更改坐标轴文字颜色
fontSize: this.fontSize(0.4) //更改坐标轴文字大小
}
},
axisLine: {
lineStyle: {
color: '#fff' //更改坐标轴颜色
}
},
data: ['00:00:00', '00:00:05', '00:00:10', '00:00:15', '00:00:20', '00:00:25', '00:00:30', '00:00:35',
'00:00:40'
]
},
yAxis: {
type: "value",
axisLabel: {
show: true,
margin: this.fontSize(0.4),
textStyle: {
color: '#fff', //更改坐标轴文字颜色
fontSize: this.fontSize(0.4) //更改坐标轴文字大小
}
},
axisLine: {
show: true,
lineStyle: {
width: 3,
color: '#fff' //更改坐标轴颜色
}
}
},
grid: {
top: '10%',
left: '10%',
bottom: this.fontSize(1)
},
tooltip: {
show: true
},
series: [{
name: 'WS001',
type: "line",
smooth: true,
label: {
color: "#fff",
fontSize: this.fontSize(0.4)
},
lineStyle: {
color: '#3FCF55',
width: 2,
type: 'solid'
}, //折线条颜色
itemStyle: {
normal: {
color: '#3FCF55'
}
}, //折线点颜色
areaStyle: {
color: {
type: 'linear',
x: 0,
y: 0,
x2: 0,
y2: 1,
colorStops: [{
offset: 0,
color: '#92FE9D' // 0% 处的颜色
}, {
offset: 1,
color: '#004B76' // 100% 处的颜色
}],
global: false // 缺省为 false
}
},
data: [0, 3, 3, 4, 5, 4, 2, 2, 0]
}]
};
C#异步编程
一.写法
先介绍一下写法,刚接触这两个关键字的时候,看别人写的代码总有些看不懂,后来发现原来是熟手们都喜欢简写,导致新手看不懂,如:
private static async Task TestFunction()
{
return await Task.Run(()=>”Test1″);
}
完整的写法应是:
private static async Task TestFunction()
{
Task task = new Task(() =>
{
return “Test1”;
});
task.Start();
var result = await task;
return result;
}
从完整的写法可以看出,await 等待的是 Task, 返回的是 Task Result 对应的具体类型值。
二.用法
下面以一些示例来说明具体用法和注意点:
1.示例,探究:只要是 async 方法里的操作,都不阻碍当前线程?
static void Main(string[] args)
{
Console.WriteLine(“Test Start”);
TestFunction();
Console.WriteLine(“Test End”);
Console.Read();
}
private static async Task TestFunction()
{
return await Task.Run(() =>
{
Thread.Sleep(2000);
string result = "Test1";
Console.WriteLine(result);
return result;
});
}
结果:
把 TestFunction 方法改成:
private static async Task TestFunction()
{
Thread.Sleep(2000);
return await Task.Run(() =>
{
string result = "Test1";
Console.WriteLine(result);
return result;
});
}
此时结果:
由此看出,Sleep 只有在 Task 或非主线程里,才不会阻碍当前UI线程,而不是只要放在 async 方法里就可以。
既然使用 Async Task,就不推荐使用 Sleep 了。一般使用 await Task.Delay, 既不影响当前主线程,也能达到延迟效果。
如:
private static async Task TestFunction()
{
await Task.Delay(5000);
string result = "Test1";
Console.WriteLine(result);
return result;
}
结果:
2.示例,探究:在 async 方法中使用多个 await
static async Task Main(string[] args)
{
await TestFunction();
Console.Read();
}
private static async Task TestFunction()
{
var task1Result = await TestFunction1();
var task2Result = await TestFunction2();
var result = $”{task1Result}+{task2Result}”;
await TestFunction3(result);
}
private static async Task TestFunction1()
{
await Task.Delay(2000);
return “TestFunction1”;
}
private static async Task TestFunction2()
{
await Task.Delay(3000);
return “TestFunction2”;
}
private static async Task TestFunction3(string str)
{
await Task.Delay(1000);
Console.WriteLine(str);
}
得到最后的结果共花了6秒:
更改 TestFunction 代码:
private static async Task TestFunction()
{
Task task1 = TestFunction1();
Task task2 = TestFunction2();
var result = await Task.WhenAll(task1, task2);
string output = string.Join("+", result);
await TestFunction3(output);
}
这样得到最后结果只花 4 秒。
总结,
1.不是遇到 async 方法就会不阻碍当前线程(因为里面可以会有Thread.Sleep…);
2.await 会阻碍当前Async Task 里面的代码执行,但不会阻塞调用 async 方法的线程;
3.如果一个任务依赖多个子任务得出结果,子任务之间又不互相依赖,可以考虑使用 WhenAll 节约时间。