Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to mark .net function with Obsolete attribute

Tags:

c#

.net

vb.net

I have created a few WebRequest extension methods that support cancelling. Is it possible to mark related .net framework methods as Obsolete. That will allow other developers to get warning and encourage them to use a new extension methods.

like image 442
walter Avatar asked Aug 08 '11 04:08

walter


People also ask

How do you mark obsolete code?

In you examples the "Method1 is deprecated" part is rather redundant. By marking it as obsolete you are saying that it is indeed obsolete, so no need to restate it in the message. Especially since the resulting warning/error will read 'Method1' is obsolete: 'Method1 is deprecated, please use Method2 instead.

What is obsolete attribute in C#?

An obsolete attribute, in C#, is a declarative tag used while declaring a type or a member of a type to indicate that it should no longer be used.

What is obsolete .NET core?

The . NET Framework indicates that a type or type member is obsolete by marking it with the ObsoleteAttribute attribute. Applying the attribute to a type or member indicates that type or member will be removed in some future version of the . NET Framework without breaking compiled code that uses that member.

Is .NET obsolete?

NET Framework will be deprecated. This means you can only use . NET Framework as long as your operating systems (for example Windows Server 2019) still supports it. And with Microsoft shortening its support lifecycles, your operating system end-of-life will come sooner than you may think.


2 Answers

If you're asking if you can mark methods from the Base Class Libraries with the Obsolete (or any) attribute, the answer is no. You must control the source for methods you want to mark up with attributes.

like image 181
Michael Petrotta Avatar answered Oct 04 '22 12:10

Michael Petrotta


You can probably redefine the .Net Framework WebRequest class in the same assembly where the extension methods are defined under System.Net namespace.

The new redefined WebRequest class can then extend the original System.Net.WebRequest class and override the existing relevant method with [Obsolete] tag.

like image 32
talo Avatar answered Oct 04 '22 12:10

talo