[C# 4.0] 1. Dynamic
2010-09-30 21:04:24 来源:WEB开发网dynamic 被编译器还原成 object,这也能理解。毕竟 C# 基于 CLR 而不是 DLR。对于新出现的 CallSite 比较好奇,就其名字来看应该是对方法调用的 "动态处理"。
注意: 为了便于阅读,下面的反编译代码中将 "<Main>o__SiteContainer0" 替换成 "SiteContainer0",将 "<>p__Site1" 之类的替换成 "site1"。
[CompilerGenerated]
private static class SiteContainer0
{
// Fields
public static CallSite<Func<CallSite, object, int, object>> site1;
public static CallSite<Func<CallSite, object, string, object>> site2;
public static CallSite<Action<CallSite, Type, object>> site3;
public static CallSite<Func<CallSite, object, object, object>> site4;
public static CallSite<Func<CallSite, object, object>> site5;
public static CallSite<Action<CallSite, Type, object>> site6;
public static CallSite<Func<CallSite, object, object, object>> site7;
public static CallSite<Func<CallSite, object, object>> site8;
}
private static void Main(string[] args)
{
object o = new MyClass();
if (SiteContainer0.site1 == null)
{
SiteContainer0.site1 = CallSite<Func<CallSite, object, int, object>>.Create
(
Binder.SetMember
(
CSharpBinderFlags.None,
"x",
typeof(Program),
new CSharpArgumentInfo[]
{
CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null),
CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.LiteralConstant |
CSharpArgumentInfoFlags.UseCompileTimeType, null)
}
)
);
}
SiteContainer0.site1.Target(SiteContainer0.site1, o, 0x7b);
if (SiteContainer0.site2 == null)
{
SiteContainer0.site2 = CallSite<Func<CallSite, object, string, object>>.Create
(
Binder.SetMember
(
CSharpBinderFlags.None,
"Y",
typeof(Program),
new CSharpArgumentInfo[]
{
CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null),
CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.LiteralConstant |
CSharpArgumentInfoFlags.UseCompileTimeType, null)
}
)
);
}
SiteContainer0.site2.Target(SiteContainer0.site2, o, "hello");
if (SiteContainer0.site3 == null)
{
SiteContainer0.site3 = CallSite<Action<CallSite, Type, object>>.Create
(
Binder.InvokeMember
(
CSharpBinderFlags.ResultDiscarded,
"WriteLine",
null,
typeof(Program),
new CSharpArgumentInfo[]
{
CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.IsStaticType |
CSharpArgumentInfoFlags.UseCompileTimeType, null),
CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null)
}
)
);
}
if (SiteContainer0.site4 == null)
{
SiteContainer0.site4 = CallSite<Func<CallSite, object, object, object>>.Create
(
Binder.InvokeMember
(
CSharpBinderFlags.None,
"Test",
null,
typeof(Program),
new CSharpArgumentInfo[]
{
CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null),
CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null)
}
)
);
}
if (SiteContainer0.site5 == null)
{
SiteContainer0.site5 = CallSite<Func<CallSite, object, object>>.Create
(
Binder.GetMember
(
CSharpBinderFlags.None,
"x",
typeof(Program),
new CSharpArgumentInfo[]
{
CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null)
}
)
);
}
SiteContainer0.site3.Target
(
SiteContainer0.site3, typeof(Console),
SiteContainer0.site4.Target
(
SiteContainer0.site4,
o,
SiteContainer0.site5.Target(SiteContainer0.site5, o)
)
);
if (SiteContainer0.site6 == null)
{
SiteContainer0.site6 = CallSite<Action<CallSite, Type, object>>.Create
(
Binder.InvokeMember
(
CSharpBinderFlags.ResultDiscarded,
"WriteLine",
null,
typeof(Program),
new CSharpArgumentInfo[]
{
CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.IsStaticType |
CSharpArgumentInfoFlags.UseCompileTimeType, null),
CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null)
}
)
);
}
if (SiteContainer0.site7 == null)
{
SiteContainer0.site7 = CallSite<Func<CallSite, object, object, object>>.Create
(
Binder.InvokeMember
(
CSharpBinderFlags.None,
"Test",
null,
typeof(Program),
new CSharpArgumentInfo[]
{
CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null),
CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null)
}
)
);
}
if (SiteContainer0.site8 == null)
{
SiteContainer0.site8 = CallSite<Func<CallSite, object, object>>.Create
(
Binder.GetMember
(
CSharpBinderFlags.None,
"Y",
typeof(Program),
new CSharpArgumentInfo[]
{
CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null)
}
)
);
}
SiteContainer0.site6.Target
(
SiteContainer0.site6,
typeof(Console),
SiteContainer0.site7.Target
(
SiteContainer0.site7,
o,
SiteContainer0.site8.Target(SiteContainer0.site8, o)
)
);
}
更多精彩
赞助商链接