The code is very simple and I will not talk about it much. Paste code and data directly.
1: Compiler call
Code
UserCore core = new UserCore ();
Core. GetCount (1, 50000279 );
2: Reflection
Code
Assembly classSampleAssembly = Assembly. LoadFrom (@ "C: \ SNSService \ Snda. Qidian. SNS. DataCore. dll ");
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: optimized reflection
Code
Assembly assembly = Assembly. Load ("Snda. Qidian. SNS. DataCore ");
Object ins = null;
Type HereType = null;
MethodInfo method = null;
// Reflection on the module
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 });
Test Data
Column B, C, and D are listed as 1, 2, and the time consumed for running 100 times in three ways
The F, G, and H columns are 1, 2, and the time consumed for one operation
The last row is the average value of each method.
Do you have any ideas.