代码很简单,我就不多说了。直接贴代码和数据。

1:编译器调用

 

 UserCore();
                core.GetCount(50000279);

 

2:反射

 

);
                Type classSampleType = classSampleAssembly.GetType("Snda.Qidian.SNS.DataCore.UserCore");
                
object instance = Activator.CreateInstance(classSampleType);
                
object returnValue1 = classSampleType.InvokeMember("GetCount", BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance, null, instance, new object[] { 50000279 });

 

3:优化后的反射

 

);
                object ins = null;
                Type HereType 
= null;
                MethodInfo method 
= null;

                
//在模块上进行反射
                foreach (Module m in assembly.GetModules())
                {

                    
foreach (Type t in m.GetTypes())
                    {
                        
if (string.Equals(t.Name, "UserCore"))
                        {
                            
foreach (MethodInfo mInfo in t.GetMethods())
                            {
                                
if (string.Equals(mInfo.Name, "GetCount"))
                                {
                                    HereType 
= t;
                                    method 
= mInfo;
                                    
break;
                                }
                            }
                        }

                        
if (method != null)
                            
break;
                    }

                    
if (method != null)
                        
break;
                }

                
if (!method.IsStatic)
                {
                    ins 
= assembly.CreateInstance(HereType.FullName);
                }
                
object RetObject = method.Invoke(ins, new object[] { 50000279 });

 

测试数据

代码运行效率的简单测试(编译器,反射,优化后的反射)

B,C,D列为第1,2,3种方式运行100次耗费的时间

F,G,H列为第1,2,3种方式运行1次耗费的时间

最后一行为各种方法的平均数值。

大家有什么想法,欢迎拍砖。

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-12-14
  • 2022-12-23
  • 2021-12-03
  • 2022-12-23
  • 2022-01-08
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-02
  • 2022-12-23
  • 2022-02-25
  • 2023-03-13
相关资源
相似解决方案