Is there way for a class to 'remove' methods that it has inherited?

后端 未结 4 1728
梦如初夏
梦如初夏 2020-12-29 22:28

Is there way for a class to \'remove\' methods that it has inherited?

E.g. if I don\'t want my class to have a ToString() method can I do something so t

4条回答
  •  心在旅途
    2020-12-29 23:06

    You can throw a NotSupportedException, mark it as Obsolete and use EditorBrowsable:

    [EditorBrowsable( EditorBrowsableState.Never )]
    [Obsolete( "...", false )]
    void YourMethod()
    {
        throw new NotSupportedException( "..." );
    }
    

    EDIT:
    As pointed out by others: I describe a way to "disable" methods, but you have to think about carefully, where you want to use this. Especially throwing exceptions can be a very dangerous thing.

提交回复
热议问题