1.牛逼的泛型类扩展方法:
/// <summary>
/// T为基础数据类型
/// (可空的 时间,数字,字符串)
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="input"></param>
/// <returns></returns>
public static string GetStringValue<T>(this T input)
{
string returnValue = string.Empty;
if (input is DateTime?)
{
var v = input as DateTime?;
return v.HasValue ? v.Value.ToString() : returnValue;
}
if (input is decimal?)
{
var v = input as decimal?;
return v.HasValue ? v.Value.ToString() : returnValue;
}
if (input is null )
return returnValue;
return input.ToString();
}
2.正则表达式
3.特性与反射
-----------------------------------------------------------------------------------------
public static Dictionary<PV,Info> GetEntityInfo<T>(this T input)
{
Dictionary<PV, Info> keyValuePairs = new Dictionary<PV, Info>();
KeyValuePair<PV, Info> keyValuePair;
var pis = input.GetType().GetProperties();
if (pis != null)
{
foreach (var pi in pis)
{
PV pV = new PV();
pV.P = pi.Name;
pV.V = pi.GetValue(input).GetStringValue();
if (pi.GetCustomAttributes(typeof(Info), true) == null||
pi.GetCustomAttributes(typeof(Info), true).Length==0)
{
Info info = new Info();
keyValuePairs.Add(pV, info);
}
else
{
foreach (Info info in pi.GetCustomAttributes(typeof(Info), true))
{
keyValuePair = new KeyValuePair<PV, Info>(pV, info);
}
}
}
}
return keyValuePairs;
}
-----------------------------------------------------------------------------------------
[AttributeUsage(AttributeTargets.Property)]
public class Info : Attribute
{
public string Display { get; set; }
public string Description { get; set; }
}
public class PV
{
public string P { get; set; }
public string V { get; set; }
}
-----------------------------------------------------------------------------------------
4.懒人最爱的linq
List<TK> tKs = new List<TK>() {
new TK(){ Name="k1",Key=10,Page=1},
new TK(){ Name="k2",Key=20,Page=1},
new TK(){ Name="pk3p",Key=30,Page=2},
new TK(){ Name="k4",Key=40,Page=2},
new TK(){ Name="k5",Key=50,Page=1}
};
var result = from r in tKs
orderby r.Key descending
group r by r.Page into n
select new
{
n.Key, //这个Key是Page
name = n.First().Name,
MaxKey = n.Max(r => r.Key)
};
5.设计模式