Attribute to generate compilation error on method call?

前端 未结 1 1054
后悔当初
后悔当初 2020-12-21 04:50

I would like to ensure that a method (actually a constructor in my case) is never called explicitly from code. It should only be called through reflection at runtime. To do

相关标签:
1条回答
  • 2020-12-21 05:05

    You can use the ObsoleteAttribute constructor that takes a boolean with which you can indicate that calling the method is a compilation error:

    [Obsolete("Don't use this", true)]
    

    However, if I were you, I'd reconsider my design, as doing this isn't a sign of a well-designed API.

    0 讨论(0)
提交回复
热议问题