创建对静态类中一组方法的访问代理

tygtug@newsmth.net发文询问“能创建一个对静态类中的一组方法的访问代理吗?”

“某些类都有一组静态方法
现在想在运行时传入类名
根据类名决定执行相应的类的静态方法。
类似于指定一个类的变量似的。”

“涉及到对旧有代码的改造时的折中考虑,才会有这个需要的,不能把问题归于类的结构涉及
不合理而搪塞阿。”

偶写了一个实现Demo:

 1 None.gif using  System;
 2 None.gif
 3 None.gif namespace  DelegateTest
 4 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 5InBlock.gif    class MainClass
 6ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 7InBlock.gif
 8InBlock.gif        [STAThread]
 9InBlock.gif        static void Main(string[] args)
10ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
11InBlock.gif            WhoAmIDelegate d1=DelegateFactory.CreateWhoAmIDelegate("DelegateTest.ClassA");
12InBlock.gif            WhoAmIDelegate d2=DelegateFactory.CreateWhoAmIDelegate("DelegateTest.ClassB");
13InBlock.gif
14InBlock.gif            Console.WriteLine(d1());
15InBlock.gif            Console.WriteLine(d2());
16InBlock.gif
17InBlock.gif            long start,end;
18InBlock.gif            int loops = 100000000;
19InBlock.gif
20InBlock.gif            start=DateTime.Now.Ticks;
21InBlock.gif            for(int i=0;i<loops;i++)
22ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
23InBlock.gif                ClassA.WhoAmI();
24ExpandedSubBlockEnd.gif            }

25InBlock.gif            end = DateTime.Now.Ticks;
26InBlock.gif            Console.WriteLine("{0}次直接调用ClassA.WhoAmI耗时:{1}ms",loops,(end-start)/10000);
27InBlock.gif
28InBlock.gif            start=DateTime.Now.Ticks;
29InBlock.gif            for(int i=0;i<loops;i++)
30ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
31InBlock.gif                d1();
32ExpandedSubBlockEnd.gif            }

33InBlock.gif            end = DateTime.Now.Ticks;
34InBlock.gif            Console.WriteLine("{0}次调用WhoAmIDelegate耗时:{1}ms",loops,(end-start)/10000);
35InBlock.gif
36ExpandedSubBlockEnd.gif        }

37ExpandedSubBlockEnd.gif    }

38InBlock.gif
39InBlock.gif
40InBlock.gif    public delegate string WhoAmIDelegate();
41InBlock.gif
42InBlock.gif
43InBlock.gif    public class ClassA
44ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
45InBlock.gif        //方法很丑陋,嘿嘿。为了性能测试方便嘛。
46InBlock.gif        public static string WhoAmI()
47ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
48InBlock.gif            return "ClassA";
49ExpandedSubBlockEnd.gif        }

50ExpandedSubBlockEnd.gif    }

51InBlock.gif
52InBlock.gif    public class ClassB
53ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
54InBlock.gif        public static string WhoAmI()
55ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
56InBlock.gif            return "ClassB";
57ExpandedSubBlockEnd.gif        }

58ExpandedSubBlockEnd.gif    }

59InBlock.gif
60InBlock.gif    public class DelegateFactory
61ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
62InBlock.gif        public static WhoAmIDelegate CreateWhoAmIDelegate(string className)
63ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
64InBlock.gif            return (WhoAmIDelegate)Delegate.CreateDelegate(Type.GetType("DelegateTest.WhoAmIDelegate"),Type.GetType(className),"WhoAmI");
65ExpandedSubBlockEnd.gif        }

66ExpandedSubBlockEnd.gif    }

67ExpandedBlockEnd.gif}

68 None.gif


Debug下运行结果:
ClassA
ClassB
100000000次直接调用ClassA.WhoAmI耗时:961ms
100000000次调用WhoAmIDelegate耗时:3134ms

Release下运行结果:
ClassA
ClassB
100000000次直接调用ClassA.WhoAmI耗时:210ms
100000000次调用WhoAmIDelegate耗时:2964ms

转载于:https://www.cnblogs.com/xiaotie/archive/2005/08/09/211073.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值