都说Reflection的性能相当差,但是非用不可的时候也得用,下面是提高反射效率的一个办法。
要被反射调用的类都实现一个接口,调用的那个类引用这个接口,将反射得来的类都转换为这个接口,然后直接调用,没有必要所有的方法都反射。

公用的接口
提高反射的调用性能namespace CommonInterface
{

提高反射的调用性能    
/// 公用的接口
提高反射的调用性能    
/// </summary>
提高反射的调用性能    public interface IFoo
{
提高反射的调用性能        
string getName();

提高反射的调用性能    }

提高反射的调用性能}

被反射调用的类
提高反射的调用性能using CommonInterface;
提高反射的调用性能
提高反射的调用性能
namespace ClassLibOne
{

提高反射的调用性能    
/// Class1 的摘要说明。
提高反射的调用性能    
/// </summary>
提高反射的调用性能    public class FooOne:IFoo
{
提高反射的调用性能        
public FooOne()
{
提高反射的调用性能        }

提高反射的调用性能        
提高反射的调用性能        
public string getName()
{
提高反射的调用性能            
return "FooOne";
提高反射的调用性能        }

提高反射的调用性能
提高反射的调用性能        
public string Name
{
提高反射的调用性能            
get
{
提高反射的调用性能                
return "FooOne";
提高反射的调用性能            }

提高反射的调用性能        }

提高反射的调用性能    }

提高反射的调用性能}


调用者类
提高反射的调用性能using System;
提高反射的调用性能
using System.Reflection;
提高反射的调用性能
using CommonInterface;
提高反射的调用性能
提高反射的调用性能
namespace ReflectionMain
{

提高反射的调用性能    
/// Class1 的摘要说明。
提高反射的调用性能    
/// </summary>
提高反射的调用性能    public class ReflectionMain
{
提高反射的调用性能        
delegate string DgetName();

提高反射的调用性能        
/// 应用程序的主入口点。
提高反射的调用性能        
/// </summary>
提高反射的调用性能        [STAThread]
提高反射的调用性能        
static void Main(string[] args)
{
提高反射的调用性能            
//加载
提高反射的调用性能
            Assembly assembly = Assembly.LoadFile("D:\\Project2003\\BankTest\\Reflection\\ReflectionTest\\ReflectionMain\\bin\\Debug\\ClassLibOne.dll");
提高反射的调用性能            Type type 
= assembly.GetType("ClassLibOne.FooOne");
);
);
提高反射的调用性能
提高反射的调用性能            
//反射调用
提高反射的调用性能
            DateTime startR = DateTime.Now;
提高反射的调用性能            
for(int i=0;i<100000;i++)
{
提高反射的调用性能                
object rtn;
);
提高反射的调用性能            }

提高反射的调用性能            TimeSpan spanR 
= DateTime.Now - startR;
提高反射的调用性能            Console.WriteLine(
"反射调用100,000次:"+spanR.ToString());
提高反射的调用性能
提高反射的调用性能
提高反射的调用性能
提高反射的调用性能            
//转换成接口后调用
提高反射的调用性能
            DateTime startI = DateTime.Now;
提高反射的调用性能            
for(int i=0;i<1000000;i++)
{
提高反射的调用性能                IFoo foo 
= (IFoo)fooOne;
提高反射的调用性能                foo.getName();
提高反射的调用性能            }

提高反射的调用性能            TimeSpan spanI 
= DateTime.Now - startI;
提高反射的调用性能            Console.WriteLine(
"转换成接口后调用1,000,000次:"+spanI.ToString());
提高反射的调用性能
提高反射的调用性能            
//用委托调用
提高反射的调用性能
            DateTime startDA = DateTime.Now;
提高反射的调用性能            IFoo fooo 
= (IFoo)fooOne;
提高反射的调用性能            DgetName dgn 
= new DgetName(fooo.getName);
提高反射的调用性能            
for(int i=0;i<10000;i++)
{
提高反射的调用性能                IFoo foo 
= (IFoo)fooOne;
提高反射的调用性能                dgn 
+= new DgetName(foo.getName);
提高反射的调用性能            }

提高反射的调用性能            TimeSpan spanDA 
= DateTime.Now - startDA;
提高反射的调用性能
提高反射的调用性能            Console.WriteLine(
"用委托调用__添加到委托10,000:"+spanDA.ToString());
提高反射的调用性能
提高反射的调用性能            DateTime startD 
= DateTime.Now;
提高反射的调用性能            dgn();
提高反射的调用性能            TimeSpan spanD 
= DateTime.Now - startD;
提高反射的调用性能
提高反射的调用性能            Console.WriteLine(
"用委托调用10,000:"+spanD.ToString());
提高反射的调用性能
提高反射的调用性能            Console.ReadLine();
提高反射的调用性能        }

提高反射的调用性能    }

提高反射的调用性能}

提高反射的调用性能



输出结果是:

提高反射的调用性能

相关文章:

  • 2021-06-03
  • 2021-12-03
  • 2022-12-23
  • 2022-03-07
  • 2021-12-18
  • 2021-11-10
  • 2021-09-23
猜你喜欢
  • 2021-06-23
  • 2022-12-23
  • 2021-09-14
  • 2021-09-03
  • 2021-08-22
相关资源
相似解决方案