1
我試圖將全類描述轉換爲字符串值。我已經指定要枚舉的類,以獲取完整描述。然而;在任何類對象上調用ToString()僅提供摘要。使用反射來獲取類描述
我想要完整的定義,就像在與它相關的類文件(.cs)中顯示的一樣。
public class Expose : System.Attribute
{
public bool DoExpose;
public Expose(bool doExpose)
{
DoExpose = doExpose;
}
public static void DoStuff()
{
Assembly assembly = Assembly.GetAssembly(typeof(Expose));
var types = GetTypesWithExposeAttribute(assembly);
// get full class description - like the Car.cs
Console.WriteLine(types.First().FullName);
}
public static IEnumerable<Type> GetTypesWithExposeAttribute(Assembly assembly)
{
foreach (Type type in assembly.GetTypes())
{
if (type.IsDefined(typeof(Expose), true))
{
yield return type;
}
}
}
}
[Expose(true)]
public class Car
{
public int CarId { get; set; }
public string Name { get; set; }
public int Speed { get; set; }
}
在[MSDN Accessing Attributes by Class](http://msdn.microsoft.com/zh-cn/library/z919e8tw.aspx)上做一些閱讀, – MethodMan 2014-11-24 17:31:43