Development With A Dot

Blog on development in general, and specifically on .NET

Sponsors

My Friends

My Links

Permanent Posts

Portuguese Communities

Yet Another Way To Create An Object

After I wrote this post, I come up with yet another way to create an object... Here it is:


Stopwatch watch = new Stopwatch();
ConstructorInfo ci = typeof(StringBuilder).GetConstructor(new Type[0]);
NewExpression expr = Expression.New(ci);
Func<StringBuilder> func = Expression.Lambda(typeof(Func<StringBuilder>), expr).Compile() as Func<StringBuilder>;

watch.Start();
			
for (Int32 i = 0; i < 100; ++i)
{
    StringBuilder builder = func();
}

Int64 time4 = watch.ElapsedTicks;

watch.Reset();

I know of only one other way, which is by using CodeDOM. If you know of any other ways to create an object, let me know!

Bookmark and Share

Comments

No Comments

Leave a Comment

(required) 

(required) 

(optional)

(required)