C# 高级使用


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.设计模式

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值