27 October 2005

Object Invocation Earliy binding vs Late binding


1) Use early binding – so the types are known at compile time.

2) Use Remoting Activator – under the hood, it is Reflection


//caller
Object instance = CreateInstanceUseReflection(myType, newArgs);
… …
private object CreateInstanceUseReflection(Type type, object[] args)
{
object instance = type.InvokeMember("",
BindingFlags.CreateInstance | BindingFlags.Public | BindingFlags.Instance,
null,
null,
args);
return instance;
}

3) Use Reflection directly

//caller
Object instance = CreateInstanceUseRemoting(myType, newArgs);
… …
private object CreateInstanceUseRemoting(Type type, object[] args)
{
return Activator.CreateInstance(type, args);
}


The test

I stress it will a search on 10,000 records against a 2 million records DB. And here is the result: (in milliseconds; timing on average of three runs each.)

use Remoting Activator 33855.69480
use Reflection 33422.52055
use Early Binding 31251.81840

Interesting to know.

No comments: