Yet Another Way To Create An Object
Posted
by Ricardo Peres
on ASP.net Weblogs
See other posts from ASP.net Weblogs
or by Ricardo Peres
Published on Fri, 02 Apr 2010 16:33:11 GMT
Indexed on
2010/04/02
16:33 UTC
Read the original article
Hit count: 344
.NET
|reflection
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!
© ASP.net Weblogs or respective owner