admin管理员组文章数量:1357276
The ObsoleteAttribute
can be used in .NET to indicate that a given member or type is going to be removed in a future version. It will trigger compiler warning 612 or 618 on the code that uses the marked identifier.
Is there anything equivalent that I can use to mark members with that are not going to be removed, but that are currently virtual and are going to become abstract in future versions? I'd like to see the compiler warn me on classes that would become uncompileable because they lack an override for the (then) abstract) member.
To be clear, I am looking for something like this:
public class MyBaseClass
{
[SoonToBeAbstract] // This is the attribute I wish for.
public virtual void DoSomething() {}
}
public class MySubClass : MyBaseClass // This is where I want to see a warning like "MySubClass does not override member DoSomething, which is going to become abstract."
{
}
Thus, the behaviour I am looking for goes quite a bit beyond merely replicating the behaviour of [Obsolete]
with a different text, as is discussed in most answers to Custom Compiler Warnings.
Can I somehow achieve this short of essentially implementing a static code analysis where I manually walk the inheritance chain myself?
The ObsoleteAttribute
can be used in .NET to indicate that a given member or type is going to be removed in a future version. It will trigger compiler warning 612 or 618 on the code that uses the marked identifier.
Is there anything equivalent that I can use to mark members with that are not going to be removed, but that are currently virtual and are going to become abstract in future versions? I'd like to see the compiler warn me on classes that would become uncompileable because they lack an override for the (then) abstract) member.
To be clear, I am looking for something like this:
public class MyBaseClass
{
[SoonToBeAbstract] // This is the attribute I wish for.
public virtual void DoSomething() {}
}
public class MySubClass : MyBaseClass // This is where I want to see a warning like "MySubClass does not override member DoSomething, which is going to become abstract."
{
}
Thus, the behaviour I am looking for goes quite a bit beyond merely replicating the behaviour of [Obsolete]
with a different text, as is discussed in most answers to Custom Compiler Warnings.
Can I somehow achieve this short of essentially implementing a static code analysis where I manually walk the inheritance chain myself?
Share Improve this question edited Mar 27 at 18:59 F-H asked Mar 27 at 17:58 F-HF-H 1,1732 gold badges18 silver badges33 bronze badges 1- If the signature of the function is going to change I think marking it as Obsolete would be appropriate. – Magnus Commented Mar 28 at 8:23
1 Answer
Reset to default 2Yes you can. Making the attribute is easy enough.
The tricky part is telling Visual Studio (or Rider or whatever IDE) what to inform the user when a class is found that will need to implement the method in future versions. You can do this with Roslyn analysers. You can even, optionally, make a "code fix" to automatically suggest a default implementation of the member. Roslyn isn't easy in my experience, but there is a good tutorial to get started here:
https://learn.microsoft/en-us/dotnet/csharp/roslyn-sdk/tutorials/how-to-write-csharp-analyzer-code-fix
And a video series here.
https://youtube/playlist?list=PLmoVFE4i0sTXwx750dJbWE577uWmEGVxY&si=9UGQIhbESMjdQ4TR
Before you start though, you should consider how this analyser will get deployed to the consumer of your library. I guess you'll bundle the analyser into a Nuget package that also contains your regular library. But that will be finicky so make a proof of concept of that part first before you put lots of effort into building the bit that analyses syntax and symbols and outputs the diagnostic.
The effort involved in that proof of concept will inform whether you want to go ahead.
本文标签: cObsolete marking for members about to become abstractStack Overflow
版权声明:本文标题:c# - Obsolete marking for members about to become abstract? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744075107a2586599.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论