一般來說,函數類A有一個屬性Atr,我想要另一個類B,Type得到它在其中註冊的類Atr。 在我的情況下,它應該是Type = typeof(A)只有沒有A. 我希望你明白。感謝您的回答。如何通過屬性定義類型?
這裏是一個示例代碼。
public class Atr: Attribute
{
public Atr()
{
DefaultDescription = "hello";
Console.WriteLine("I am here. I'm the attribute constructor!");
}
public String CustomDescription { get; set; }
public String DefaultDescription { get; set; }
public override String ToString()
{
return String.Format("Custom: {0}; Default: {1}", CustomDescription, DefaultDescription);
}
}
class B
{
public void Laun()
{
Type myType = typeof(A); // хочу получить тоже самое только через Atr
}
}
class A
{
[Atr]
public static void func(int a, int b)
{
Console.WriteLine("a={0} b={1}",a,b);
}
}
完全不清楚你在問什麼,你想檢查'func'方法是否有'Atr'屬性? –
我想確定具有該屬性的類的類型。 例如,如果我調用任何具有屬性的類中的任何方法,請檢查Atr確定類型。 – askeet