Yet Another Way To Create An Object
- by Ricardo Peres
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!
SyntaxHighlighter.config.clipboardSwf = 'http://alexgorbatchev.com/pub/sh/2.0.320/scripts/clipboard.swf';
SyntaxHighlighter.brushes.CSharp.aliases = ['c#', 'c-sharp', 'csharp'];
SyntaxHighlighter.all();