使用.net Refector 反編譯C#,的确是一個很好的工具,不過随着研究的深入發現,其中還有有一些不盡人意,讓初學者摸不着頭腦的情況。
源碼
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.IO;
namespace ConsoleApplication1
{
class Student
{
public int ID { get; set; }
public string Name { get; set; }
}
class Program
{
static void Main(string[] args)
{
// Source
var student = new Student { ID = 1, Name = "jello" };
// Dynamic Assign
dynamic d = student;
Console.WriteLine(d.Name);
Console.ReadKey();
}
}
}
反編譯後的效果
internal class Student
{
// Properties
public int ID { get; set; }
public string Name { get; set; }
}
internal class Program
{
// Methods
private static void Main(string[] args)
{
CSharpArgumentInfo[] infoArray;
Student student2 = new Student {
ID = 1,
Name = "jello"
};
object obj2 = student2;
if (<Main>o__SiteContainer1.<>p__Site2 == null)
{
infoArray = new CSharpArgumentInfo[] { CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.IsStaticType | CSharpArgumentInfoFlags.UseCompileTimeType, null), CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null) };
<Main>o__SiteContainer1.<>p__Site2 = CallSite<Action<CallSite, Type, object>>.Create(Binder.InvokeMember(CSharpBinderFlags.ResultDiscarded, "WriteLine", null, typeof(Program), infoArray));
}
if (<Main>o__SiteContainer1.<>p__Site3 == null)
{
infoArray = new CSharpArgumentInfo[] { CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null) };
<Main>o__SiteContainer1.<>p__Site3 = CallSite<Func<CallSite, object, object>>.Create(Binder.GetMember(CSharpBinderFlags.None, "Name", typeof(Program), infoArray));
}
<Main>o__SiteContainer1.<>p__Site2.Target(<Main>o__SiteContainer1.<>p__Site2, typeof(Console), <Main>o__SiteContainer1.<>p__Site3.Target(<Main>o__SiteContainer1.<>p__Site3, obj2));
Console.ReadKey();
}
}
Student結構體很清晰,不過一個dynamic 關鍵字就很複雜啦!