C#扩展方法知多少
2017-04-09 10:25
197 查看
前言:上篇 序列化效率比拼——谁是最后的赢家Newtonsoft.Json 介绍了下序列化方面的知识。看过Demo的朋友可能注意到了里面就用到过泛型的扩展方法,本篇打算总结下C#扩展方法的用法。博主打算分三个层面来介绍这个知识点,分别是:.Net内置对象的扩展方法、一般对象的扩展方法、泛型对象的扩展方法。
什么是扩展方法?回答这个问题之前,先看看我们一般情况下方法的调用。类似这样的通用方法你一定写过:
这样看上去就像在扩展.Net Framework。有没有感觉有点高大上~
2、一般对象的扩展方法
和Framework内置对象一样,自定义的对象也可以增加扩展方法。直接上示例代码:
Main方法里面调用:
和string扩展方法类似,就不多做解释了。
3、泛型对象的扩展方法
除了上面两种之外,博主发现其实可以定义一个泛型的扩展方法。那么,是不是所有的类型都可以直接使用这个扩展方法了呢?为了保持程序的严谨,下面的方法可能没有实际意义,当开发中博主觉得可能存在这种场景:
使用扩展方法有几个值得注意的地方:
(1)扩展方法不能和调用的方法放到同一个类中
(2)第一个参数必须要,并且必须是this,这是扩展方法的标识。如果方法里面还要传入其他参数,可以在后面追加参数
(3)扩展方法所在的类必须是静态类
(4)最好保证扩展方法和调用方法在同一个命名空间下
可能你第一次使用这个会觉得很别扭。你也许会说扩展方法和我以前用的static方法无论从代码实现还是算法效率都差不多嘛,是的!确实差不多,但使用多了之后会发现它确实能帮你省去很多代码。
什么是扩展方法?回答这个问题之前,先看看我们一般情况下方法的调用。类似这样的通用方法你一定写过:
//DataGridViewRow的扩展方法,将当前选中行转换为对应的对象 public static T ToObject<T>(this DataGridViewRow item) where T:class { var model = item.DataBoundItem as T; if (model != null) return model; var dr = item.DataBoundItem as System.Data.DataRowView; model = (T)typeof(T).GetConstructor(new System.Type[] { }).Invoke(new object[] { });//反射得到泛型类的实体 PropertyInfo[] pro = typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public); Type type = model.GetType(); foreach (PropertyInfo propertyInfo in pro) { if (Convert.IsDBNull(dr[propertyInfo.Name])) { continue; } if (!string.IsNullOrEmpty(Convert.ToString(dr[propertyInfo.Name]))) { var propertytype = propertyInfo.PropertyType; } } return model; }
//DataGridViewRow的扩展方法,将当前选中行转换为对应的对象 public static T ToObject<T>(this DataGridViewRow item) where T:class { var model = item.DataBoundItem as T; if (model != null) return model; var dr = item.DataBoundItem as System.Data.DataRowView; model = (T)typeof(T).GetConstructor(new System.Type[] { }).Invoke(new object[] { });//反射得到泛型类的实体 PropertyInfo[] pro = typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public); Type type = model.GetType(); foreach (PropertyInfo propertyInfo in pro) { if (Convert.IsDBNull(dr[propertyInfo.Name])) { continue; } if (!string.IsNullOrEmpty(Convert.ToString(dr[propertyInfo.Name]))) { var propertytype = propertyInfo.PropertyType; } } return model; }
这样看上去就像在扩展.Net Framework。有没有感觉有点高大上~
2、一般对象的扩展方法
和Framework内置对象一样,自定义的对象也可以增加扩展方法。直接上示例代码:
public class Person { public string Name { set; get; } public int Age { set; get; } }
//Person的扩展方法,根据年龄判断是否是成年人 public static bool GetBIsChild(this Person oPerson) { if (oPerson.Age >= 18) return false; else return true; }
Main方法里面调用:
var oPerson1 = new Person(); oPerson1.Age = 20; var bIsChild = oPerson1.GetBIsChild();
和string扩展方法类似,就不多做解释了。
3、泛型对象的扩展方法
除了上面两种之外,博主发现其实可以定义一个泛型的扩展方法。那么,是不是所有的类型都可以直接使用这个扩展方法了呢?为了保持程序的严谨,下面的方法可能没有实际意义,当开发中博主觉得可能存在这种场景:
public static class DataContractExtensions { //测试方法 public static T Test<T>(this T instance) where T : Test2 { T Res = default(T); try { Res.AttrTest = instance.AttrTest.Substring(0,2); //其他复杂逻辑... } catch { } return Res; } } public class Test2 { public string AttrTest { set; get; } }
使用扩展方法有几个值得注意的地方:
(1)扩展方法不能和调用的方法放到同一个类中
(2)第一个参数必须要,并且必须是this,这是扩展方法的标识。如果方法里面还要传入其他参数,可以在后面追加参数
(3)扩展方法所在的类必须是静态类
(4)最好保证扩展方法和调用方法在同一个命名空间下
可能你第一次使用这个会觉得很别扭。你也许会说扩展方法和我以前用的static方法无论从代码实现还是算法效率都差不多嘛,是的!确实差不多,但使用多了之后会发现它确实能帮你省去很多代码。
相关文章推荐
- C#扩展方法知多少
- C#扩展方法知多少
- C#扩展方法知多少
- 从C# 3.0说以人为本(三)—— 扩展方法
- 不能不说的C#特性-扩展方法
- C# 3.0新特性初步研究 Part2:使用扩展方法_C#教程
- C# 3.0新特性初步研究 Part2:使用扩展方法_C#教程
- C# 中的扩展方法---Extension methods in C#
- 实例 C#3.0与C#2.0相比之新特性(二)---扩展方法
- C#3.5语言新特性(四)-扩展方法
- C#2008新特性--(扩展方法)Extension Method
- 向C#的String类添加按字节截取字符串的扩展方法
- C# 3.0语言新特性(语言规范):2 扩展方法
- 向C#的String类添加按字节截取字符串的扩展方法
- c#中的扩展方法
- Linq的那些事——从Linq扩展方法回顾C#语言基础
- C# 3.0新特性初步研究 Part2:使用扩展方法_C#教程
- 初探C# 3.0 (1) - 隐式类型变量, 扩展方法, Lambda表达式
- C# 3.0 扩展方法 实践
- 向C#的String类添加按字节截取字符串的扩展方法