在創(chuàng)建“AttributeTargets.Parameter”構(gòu)造函數(shù)的自定義屬性時(shí)不會調(diào)用。我想在Test Class下使用Fn函數(shù)的參數(shù)值。我使用了 .net 核心和 .net 標(biāo)準(zhǔn)。class Program{ public static void Main() { var test = new Test(); test.Fn("Hello"); Console.WriteLine("end"); Console.Read(); }}public class Test{ public void Fn([Parameter] string parm) { Console.WriteLine(parm); }}[AttributeUsage(AttributeTargets.Parameter)]public class ParameterAttribute : Attribute{ public ParameterAttribute() { Console.WriteLine("In Parameter Attribute"); }}
1 回答

大話西游666
TA貢獻(xiàn)1817條經(jīng)驗(yàn) 獲得超14個贊
據(jù)我所知,屬性構(gòu)造函數(shù)僅在您開始檢查類型時(shí)執(zhí)行,而不是在創(chuàng)建該類型的實(shí)例或執(zhí)行方法時(shí)(在您的情況下)。
您可以查看此答案,了解使用自定義Attributes時(shí)執(zhí)行順序的詳細(xì)示例。
希望能幫助到你!
- 1 回答
- 0 關(guān)注
- 268 瀏覽
添加回答
舉報(bào)
0/150
提交
取消