主题:这个有问题吗 大家帮忙啊..........
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TestDelegate
{
delegate void MyDelegate();
class Myclass
{
public static void staticMethod1()
{
Console.WriteLine("第一静态方法");
}
public static void staticMethod2()
{
Console.WriteLine("第2静态方法");
}
public static void InstanceMethod1()
{
Console.WriteLine("第1个实例方法");
}
public static void InstanceMethod2()
{
Console.WriteLine("第2个实例方法");
}
}
class class1
{
static void DispDelegate( MyDelegate d )
{
Console.WriteLine();
Console.WriteLine("----------------------------");
Console.WriteLine("代理{0}的调用链表:",d );
Delegate []ds =d.GetInvocationList();
for (int i=0;i<ds.Length; i++)
{
Console.WriteLine("第{0}个元素的目标为:{1}",i,
ds[i].Target==null?"null":ds[i].Target);
Console.WriteLine("引用方法为:{0}",ds[i].Method);
}
Console.WriteLine("当前目标为{0},引用方法为{1}",
d.Target==null?"null":d.Target,
d.Method);
}
static void Main (string[] args)
{
MyClass myObj = new MyClass();
MyDelegate d1 = new MyDelegate(MyClass.StaticMethod1);
DispDelegate (d1);
d1 +=new MyDelegate (MyClass.StaticMethod2);
d1 +=new MyDelegate (myObj.InstanceMethod1);
d1 +=new MyDelegate (myObj.InstanceMethod2);
DispDelegate(d1);
d1 -=new MyDelegate (MyObj.IntanceMethod1);
DispDelegate(d1);
Console.WriteLine();
Console.WriteLine("代理对象d2和d3的目标和引用方法相同");
MyDelegate d2 =new MyDelegate (MyClass.StaticMethod1);
MyDelegate d3 =new MyDelegate (MyClass.StaticMethod1);
Console.WriteLine("代理对象d2{0} d3",d2==d3?"==":"!=");
d2 +=new MyDelegate (MyClass.StaticMethod2);
d2 +=new MyDelegate (myObj.InstanceMethod1);
d2 +=new MyDelegate (myObj.InstanceMethod2);
d3 +=new MyDelegate (MyClass.StaticMethod1);
d3 +=new MyDelegate (myObj.InstanceMethod2);
d3 +=new MyDelegate (myObj.InstanceMethod2);
Console.WriteLine("代理对象d2和d3的调用列表的只有顺寻不同");
Console.WriteLine("代理对象d2{0} d3",d2==d3?"==":"!=");
d2 -=new MyDelegate (MyObj.IntanceMethod1);
Console.WriteLine("代理对象d1和d2的目标和引用方法相同");
Console.WriteLine("代理对象d1 {0} d2",d1==d2?"==":"!=");
MyDelegate d4 =d2+d3;
DispDelegate(d4);
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TestDelegate
{
delegate void MyDelegate();
class Myclass
{
public static void staticMethod1()
{
Console.WriteLine("第一静态方法");
}
public static void staticMethod2()
{
Console.WriteLine("第2静态方法");
}
public static void InstanceMethod1()
{
Console.WriteLine("第1个实例方法");
}
public static void InstanceMethod2()
{
Console.WriteLine("第2个实例方法");
}
}
class class1
{
static void DispDelegate( MyDelegate d )
{
Console.WriteLine();
Console.WriteLine("----------------------------");
Console.WriteLine("代理{0}的调用链表:",d );
Delegate []ds =d.GetInvocationList();
for (int i=0;i<ds.Length; i++)
{
Console.WriteLine("第{0}个元素的目标为:{1}",i,
ds[i].Target==null?"null":ds[i].Target);
Console.WriteLine("引用方法为:{0}",ds[i].Method);
}
Console.WriteLine("当前目标为{0},引用方法为{1}",
d.Target==null?"null":d.Target,
d.Method);
}
static void Main (string[] args)
{
MyClass myObj = new MyClass();
MyDelegate d1 = new MyDelegate(MyClass.StaticMethod1);
DispDelegate (d1);
d1 +=new MyDelegate (MyClass.StaticMethod2);
d1 +=new MyDelegate (myObj.InstanceMethod1);
d1 +=new MyDelegate (myObj.InstanceMethod2);
DispDelegate(d1);
d1 -=new MyDelegate (MyObj.IntanceMethod1);
DispDelegate(d1);
Console.WriteLine();
Console.WriteLine("代理对象d2和d3的目标和引用方法相同");
MyDelegate d2 =new MyDelegate (MyClass.StaticMethod1);
MyDelegate d3 =new MyDelegate (MyClass.StaticMethod1);
Console.WriteLine("代理对象d2{0} d3",d2==d3?"==":"!=");
d2 +=new MyDelegate (MyClass.StaticMethod2);
d2 +=new MyDelegate (myObj.InstanceMethod1);
d2 +=new MyDelegate (myObj.InstanceMethod2);
d3 +=new MyDelegate (MyClass.StaticMethod1);
d3 +=new MyDelegate (myObj.InstanceMethod2);
d3 +=new MyDelegate (myObj.InstanceMethod2);
Console.WriteLine("代理对象d2和d3的调用列表的只有顺寻不同");
Console.WriteLine("代理对象d2{0} d3",d2==d3?"==":"!=");
d2 -=new MyDelegate (MyObj.IntanceMethod1);
Console.WriteLine("代理对象d1和d2的目标和引用方法相同");
Console.WriteLine("代理对象d1 {0} d2",d1==d2?"==":"!=");
MyDelegate d4 =d2+d3;
DispDelegate(d4);
}
}
}