1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6 using OtherNamespce;
7
8
9 namespace CurrentNamespace
10 {
11 class Program
12 {
13 static void Main(string[] args)
14 {
15 Person p = new Person { Name = "xcl" };
16 p.Print();//错误 1 在以下方法或属性之间的调用不明确:
17 Console.Read();
18
19 }
20 }
21
22 //自定义类型
23 public class Person
24 {
25 public string Name { get; set; }
26 }
27 //当前命名空间下扩展方法的定义
28 public static class Extensionclass1 //扩展方法必须在一个非嵌套,非泛型 的静态类中定义
29 { //至少有一个参数;//第一个参数必须加上this关键字,第一个参数类型也称为扩展类型,就是方法对这个类型进行扩展,
30 //第一个参数不能使用任何修饰符,如 :ref,out等修饰符
31 //第一个参数不能是指针类型
32 //扩展方法必须是静态的
33 //public static void Print(this Person per)
34 //{
35 // Console.WriteLine("调用的是当前命名空间下的扩展方法输出,姓名为:{0}", per.Name);
36 //}
37 }
38 //又一个当期命名空间下的扩展方法定义
39 //public static class Extensionclass2
40 //{
41 // //同一个命名空间下,定义了相同的Print扩展方法
42 // public static void Print(this Person per)
43 // {
44 // Console.WriteLine("调用的是当期命名下的扩展方法输出,姓名为:{0}", per.Name);
45 // }
46 //}
47 }
48
49 //怎么解决这个问题 ?
50 //1. 同一个命名空间下,相同的扩展名方法名字改成为不同的;
51 //2. 把相同的方法名字改到不同的命名空间;
52 namespace OtherNamespce
53 {
54 using CurrentNamespace;
55 public static class Extensionclass2
56 {
57 //同一个命名空间下,定义了两个相同的Print扩展方法
58 public static void Print(this Person per)
59 {
60 Console.WriteLine("调用的是 OtherNamespce 命名下的扩展方法输出,姓名为:{0}", per.Name);
61 }
62 }
63 }