why invoking a method by reflection is much slower than making a interface then recall it by reflection. the first version shows the tedious way the other version shows the enhanced way??
// first version
class A
{
public void fn()
{
}
}
void Main(String[]x)
{
Type type = typeof(A);
object obj = Activator.CreateInstance(type);
type.InvokeMember("fn", BindingFlags.Public, null, obj, null);
}
//second verison
interface IA
{
void fn();
}
class A :IA
{
public void fn()
{
}
}
void Main(String []x)
{
Type type = typeof(A);
IA obj =(IA) Activator.CreateInstance(type);
obj.fn();
}
Reflection-based method calls are extremely slow, since you need to do member lookup and parameter binding and other things at runtime.
Interface methods, by contrast, are called with a regular
callvirtinstruction using the vtable.