如果你只是想生成像你所示的僞接口代碼,你c
string GetSourceCode(Type t)
{
var sb = new StringBuilder();
sb.AppendFormat("public class {0}\n{{\n", t.Name);
foreach (var field in t.GetFields())
{
sb.AppendFormat(" public {0} {1};\n",
field.FieldType.Name,
field.Name);
}
foreach (var prop in t.GetProperties())
{
sb.AppendFormat(" public {0} {1} {{{2}{3}}}\n",
prop.PropertyType.Name,
prop.Name,
prop.CanRead ? " get;" : "",
prop.CanWrite ? " set; " : " ");
}
sb.AppendLine("}");
return sb.ToString();
}
對於類型:
public class MyType
{
public int test;
public string Name { get; set; }
public int Age { get; set; }
public int ReadOnly { get { return 1; } }
public int SetOnly { set {} }
}
輸出是:在公共領域&這類似一個迭代
public class MyType
{
public Int32 test;
public String Name { get; set; }
public Int32 Age { get; set; }
public Int32 ReadOnly { get; }
public Int32 SetOnly { set; }
}
您可以通過使用側步的問題,現有的像ILSpy這樣的反編譯器。 – CodesInChaos 2012-02-01 22:59:15
但列舉所有屬性,檢查是否有一個getter/setter和打印他們的類型也不應該很難。 – CodesInChaos 2012-02-01 23:00:05