Last active
December 14, 2015 15:08
-
-
Save hotgazpacho/5105456 to your computer and use it in GitHub Desktop.
Dynamic Iteration Repo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.CSharp.RuntimeBinder; | |
using System; | |
using System.Collections.Generic; | |
using System.Dynamic; | |
using System.Linq; | |
using System.Runtime.CompilerServices; | |
namespace DynamicCompilation | |
{ | |
public class DynamicCompilation | |
{ | |
// | |
// Methods | |
// | |
public void DynamicEnumeration () | |
{ | |
List<object> list = Enumerable.Range (1, 100).Select (delegate (int x) | |
{ | |
object obj = new ExpandoObject (); | |
if (DynamicCompilation.<<DynamicEnumeration>m__1>c__DynamicSite2.Site0 == null) | |
{ | |
DynamicCompilation.<<DynamicEnumeration>m__1>c__DynamicSite2.Site0 = CallSite<Action<CallSite, object, int>>.Create (Binder.SetMember (CSharpBinderFlags.None, "Id", typeof(DynamicCompilation), new CSharpArgumentInfo[] | |
{ | |
CSharpArgumentInfo.Create (CSharpArgumentInfoFlags.None, null), | |
CSharpArgumentInfo.Create (CSharpArgumentInfoFlags.UseCompileTimeType, null) | |
})); | |
} | |
DynamicCompilation.<<DynamicEnumeration>m__1>c__DynamicSite2.Site0.Target.Invoke (DynamicCompilation.<<DynamicEnumeration>m__1>c__DynamicSite2.Site0, obj, x); | |
if (DynamicCompilation.<<DynamicEnumeration>m__1>c__DynamicSite2.Site1 == null) | |
{ | |
DynamicCompilation.<<DynamicEnumeration>m__1>c__DynamicSite2.Site1 = CallSite<Action<CallSite, object, string>>.Create (Binder.SetMember (CSharpBinderFlags.None, "Name", typeof(DynamicCompilation), new CSharpArgumentInfo[] | |
{ | |
CSharpArgumentInfo.Create (CSharpArgumentInfoFlags.None, null), | |
CSharpArgumentInfo.Create (CSharpArgumentInfoFlags.UseCompileTimeType, null) | |
})); | |
} | |
DynamicCompilation.<<DynamicEnumeration>m__1>c__DynamicSite2.Site1.Target.Invoke (DynamicCompilation.<<DynamicEnumeration>m__1>c__DynamicSite2.Site1, obj, string.Format ("Name{0}", x)); | |
return obj; | |
}).ToList<object> (); | |
using (List<object>.Enumerator enumerator = list.GetEnumerator ()) | |
{ | |
while (enumerator.MoveNext ()) | |
{ | |
object current = enumerator.get_Current (); | |
if (DynamicCompilation.<DynamicEnumeration>c__DynamicSite0.Site0 == null) | |
{ | |
DynamicCompilation.<DynamicEnumeration>c__DynamicSite0.Site0 = CallSite<Action<CallSite, DynamicCompilation, object>>.Create (Binder.InvokeMember (CSharpBinderFlags.InvokeSimpleName | CSharpBinderFlags.ResultDiscarded, "Print", null, typeof(DynamicCompilation), new CSharpArgumentInfo[] | |
{ | |
CSharpArgumentInfo.Create (CSharpArgumentInfoFlags.UseCompileTimeType, null), | |
CSharpArgumentInfo.Create (CSharpArgumentInfoFlags.None, null) | |
})); | |
} | |
DynamicCompilation.<DynamicEnumeration>c__DynamicSite0.Site0.Target.Invoke (DynamicCompilation.<DynamicEnumeration>c__DynamicSite0.Site0, this, current); | |
} | |
} | |
} | |
public void StaticIteration () | |
{ | |
List<StaticObject> list = ( | |
from x in Enumerable.Range (1, 100) | |
select new StaticObject | |
{ | |
Id = x, | |
Name = string.Format ("Name{0}", x) | |
}).ToList<StaticObject> (); | |
using (List<StaticObject>.Enumerator enumerator = list.GetEnumerator ()) | |
{ | |
while (enumerator.MoveNext ()) | |
{ | |
StaticObject current = enumerator.get_Current (); | |
this.Print (current); | |
} | |
} | |
} | |
// | |
// Nested Types | |
// | |
[CompilerGenerated] | |
private static class <<DynamicEnumeration>m__1>c__DynamicSite2 | |
{ | |
public static CallSite<Action<CallSite, object, int>> Site0; | |
public static CallSite<Action<CallSite, object, string>> Site1; | |
} | |
[CompilerGenerated] | |
private static class <DynamicEnumeration>c__DynamicSite0 | |
{ | |
public static CallSite<Action<CallSite, DynamicCompilation, object>> Site0; | |
} | |
[CompilerGenerated] | |
private static class <Print>c__DynamicSite1 | |
{ | |
public static CallSite<Action<CallSite, Type, string, object, object>> Site0; | |
public static CallSite<Func<CallSite, object, object>> Site1; | |
public static CallSite<Func<CallSite, object, object>> Site2; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Linq; | |
using System.Collections.Generic; | |
using System.Dynamic; | |
namespace DynamicCompilation | |
{ | |
public class StaticObject | |
{ | |
public int Id { | |
get; | |
set; | |
} | |
public string Name { | |
get; | |
set; | |
} | |
} | |
public class DynamicCompilation | |
{ | |
public void StaticIteration() | |
{ | |
var staticObjects = Enumerable.Range(1,100).Select(x => new StaticObject {Id = x, Name = string.Format("Name{0}", x)}).ToList(); | |
foreach (var item in staticObjects) { | |
Print(item); | |
} | |
} | |
public void DynamicEnumeration() | |
{ | |
var dynamicObjects = Enumerable.Range(1,100) | |
.Select(x => { | |
dynamic d = new ExpandoObject(); | |
d.Id = x; | |
d.Name = string.Format("Name{0}", x); | |
return d; | |
}).ToList(); | |
foreach (var item in dynamicObjects) { | |
Print(item); | |
} | |
} | |
void Print (dynamic item) | |
{ | |
Console.WriteLine("Static Object: {0}, {1}", item.Id, item.Name); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Compile this, then use a tool like dotPeek to look at the decompiled (don't use the PDB!) sources. What I have found is that the code called inside the foreach loops in each method is different.