创建对静态类中一组方法的访问代理
tygtug@newsmth.net发文询问“能创建一个对静态类中的一组方法的访问代理吗?”
“某些类都有一组静态方法
现在想在运行时传入类名
根据类名决定执行相应的类的静态方法。
类似于指定一个类的变量似的。”
“涉及到对旧有代码的改造时的折中考虑,才会有这个需要的,不能把问题归于类的结构涉及
不合理而搪塞阿。”
偶写了一个实现Demo:
1using System;
2
3namespace DelegateTest
4{
5 class MainClass
6 {
7
8 [STAThread]
9 static void Main(string[] args)
10 {
11 WhoAmIDelegate d1=DelegateFactory.CreateWhoAmIDelegate("DelegateTest.ClassA");
12 WhoAmIDelegate d2=DelegateFactory.CreateWhoAmIDelegate("DelegateTest.ClassB");
13
14 Console.WriteLine(d1());
15 Console.WriteLine(d2());
16
17 long start,end;
18 int loops = 100000000;
19
20 start=DateTime.Now.Ticks;
21 for(int i=0;i<loops;i++)
22 {
23 ClassA.WhoAmI();
24 }
25 end = DateTime.Now.Ticks;
26 Console.WriteLine("{0}次直接调用ClassA.WhoAmI耗时:{1}ms",loops,(end-start)/10000);
27
28 start=DateTime.Now.Ticks;
29 for(int i=0;i<loops;i++)
30 {
31 d1();
32 }
33 end = DateTime.Now.Ticks;
34 Console.WriteLine("{0}次调用WhoAmIDelegate耗时:{1}ms",loops,(end-start)/10000);
35
36 }
37 }
38
39
40 public delegate string WhoAmIDelegate();
41
42
43 public class ClassA
44 {
45 //方法很丑陋,嘿嘿。为了性能测试方便嘛。
46 public static string WhoAmI()
47 {
48 return "ClassA";
49 }
50 }
51
52 public class ClassB
53 {
54 public static string WhoAmI()
55 {
56 return "ClassB";
57 }
58 }
59
60 public class DelegateFactory
61 {
62 public static WhoAmIDelegate CreateWhoAmIDelegate(string className)
63 {
64 return (WhoAmIDelegate)Delegate.CreateDelegate(Type.GetType("DelegateTest.WhoAmIDelegate"),Type.GetType(className),"WhoAmI");
65 }
66 }
67}
68
2
3namespace DelegateTest
4{
5 class MainClass
6 {
7
8 [STAThread]
9 static void Main(string[] args)
10 {
11 WhoAmIDelegate d1=DelegateFactory.CreateWhoAmIDelegate("DelegateTest.ClassA");
12 WhoAmIDelegate d2=DelegateFactory.CreateWhoAmIDelegate("DelegateTest.ClassB");
13
14 Console.WriteLine(d1());
15 Console.WriteLine(d2());
16
17 long start,end;
18 int loops = 100000000;
19
20 start=DateTime.Now.Ticks;
21 for(int i=0;i<loops;i++)
22 {
23 ClassA.WhoAmI();
24 }
25 end = DateTime.Now.Ticks;
26 Console.WriteLine("{0}次直接调用ClassA.WhoAmI耗时:{1}ms",loops,(end-start)/10000);
27
28 start=DateTime.Now.Ticks;
29 for(int i=0;i<loops;i++)
30 {
31 d1();
32 }
33 end = DateTime.Now.Ticks;
34 Console.WriteLine("{0}次调用WhoAmIDelegate耗时:{1}ms",loops,(end-start)/10000);
35
36 }
37 }
38
39
40 public delegate string WhoAmIDelegate();
41
42
43 public class ClassA
44 {
45 //方法很丑陋,嘿嘿。为了性能测试方便嘛。
46 public static string WhoAmI()
47 {
48 return "ClassA";
49 }
50 }
51
52 public class ClassB
53 {
54 public static string WhoAmI()
55 {
56 return "ClassB";
57 }
58 }
59
60 public class DelegateFactory
61 {
62 public static WhoAmIDelegate CreateWhoAmIDelegate(string className)
63 {
64 return (WhoAmIDelegate)Delegate.CreateDelegate(Type.GetType("DelegateTest.WhoAmIDelegate"),Type.GetType(className),"WhoAmI");
65 }
66 }
67}
68
Debug下运行结果:
ClassA
ClassB
100000000次直接调用ClassA.WhoAmI耗时:961ms
100000000次调用WhoAmIDelegate耗时:3134ms
Release下运行结果:
ClassA
ClassB
100000000次直接调用ClassA.WhoAmI耗时:210ms
100000000次调用WhoAmIDelegate耗时:2964ms
版权所有,欢迎转载