473,809 Members | 2,617 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Obsolete attribute in a derived class

I am trying to mark an override method in a derived class as obsolete using
the ObsoleteAttribu te. The compiler, however is not picking up this
attribute and is not generating a warning or an error.

Sample code is below:

If I put the attribute on the static method DoSomething() in the main class,
a warning/error is generated by the compiler.

If I put the attribute on the virtual instance method DoSomething() of
Class2, a warning/error is generated.

If I put the attribute on the override instance method DoSomething() of
Class3 ( which is derived from Class2), no warning or error is generated by
the compiler.

Is this a bug, or is there some good reason for this? It certainly isn't
documented. Any ideas?
using System;
namespace ConsoleApplicat ion
{

class Class1
{
[STAThread]
static void Main(string[] args)
{
Class2 o = new Class2();
o.DoSomething() ;

o = new Class3();
o.DoSomething() ;

DoSomething();

Console.ReadLin e();
}

[Obsolete("Funct ion 'DoSomething()' is obsolete.", true )]
static void DoSomething()
{
Console.WriteLi ne( "I've done something.");
}
}

public class Class2
{
public Class2() {}

public virtual void DoSomething()
{
Console.WriteLi ne("Class2.DoSo mething()");
}
}

public class Class3 : Class2
{
public Class3(){}

[Obsolete("Use somethiing else instead.", true)]
public override void DoSomething()
{
Console.WriteLi ne("Class3.DoSo mething()");
}
}

}

Thanks,

Steve
Nov 15 '05 #1
2 5045
If you look into the il generated, you'll notice the line :
callvirt instance void
ConsoleApplicat ion1.ConsoleApp lication.Class2 ::DoSomething()
For both calls to o.DoSomething() .
Even if you turn it into
Class3 o = new Class3();
o.DoSomething() ;

the il still reads the same. Because Class2.DoSometh ing is virtual, the
instance call to Classxxx.DoSome thing() will always be a callvirt to
Class2.DoSometh ing, as long as Classxxx has Class2 as an ancestor. (unless
you declare DoSomething with "new")

This is why the obsolete isn't being applied -- class3.DoSometh ing() is not
actually called directly.


"Steve James" <st***********@ ntlworld.com> wrote in message
news:Rb******** *****@newsfep3-gui.server.ntli .net...
I am trying to mark an override method in a derived class as obsolete using the ObsoleteAttribu te. The compiler, however is not picking up this
attribute and is not generating a warning or an error.

Sample code is below:

If I put the attribute on the static method DoSomething() in the main class, a warning/error is generated by the compiler.

If I put the attribute on the virtual instance method DoSomething() of
Class2, a warning/error is generated.

If I put the attribute on the override instance method DoSomething() of
Class3 ( which is derived from Class2), no warning or error is generated by the compiler.

Is this a bug, or is there some good reason for this? It certainly isn't
documented. Any ideas?
using System;
namespace ConsoleApplicat ion
{

class Class1
{
[STAThread]
static void Main(string[] args)
{
Class2 o = new Class2();
o.DoSomething() ;

o = new Class3();
o.DoSomething() ;

DoSomething();

Console.ReadLin e();
}

[Obsolete("Funct ion 'DoSomething()' is obsolete.", true )]
static void DoSomething()
{
Console.WriteLi ne( "I've done something.");
}
}

public class Class2
{
public Class2() {}

public virtual void DoSomething()
{
Console.WriteLi ne("Class2.DoSo mething()");
}
}

public class Class3 : Class2
{
public Class3(){}

[Obsolete("Use somethiing else instead.", true)]
public override void DoSomething()
{
Console.WriteLi ne("Class3.DoSo mething()");
}
}

}

Thanks,

Steve

Nov 15 '05 #2
I suppose, when you think about it, why would a warning or error be raised
at compile-time, on a method call bound at run-time?

Thanks, Philip. I'll look at the IL next time.

Steve

"Philip Rieck" <st***@mckraken .com> wrote in message
news:ep******** ******@tk2msftn gp13.phx.gbl...
If you look into the il generated, you'll notice the line :
callvirt instance void
ConsoleApplicat ion1.ConsoleApp lication.Class2 ::DoSomething()
For both calls to o.DoSomething() .
Even if you turn it into
Class3 o = new Class3();
o.DoSomething() ;

the il still reads the same. Because Class2.DoSometh ing is virtual, the
instance call to Classxxx.DoSome thing() will always be a callvirt to
Class2.DoSometh ing, as long as Classxxx has Class2 as an ancestor. (unless
you declare DoSomething with "new")

This is why the obsolete isn't being applied -- class3.DoSometh ing() is not actually called directly.


"Steve James" <st***********@ ntlworld.com> wrote in message
news:Rb******** *****@newsfep3-gui.server.ntli .net...
I am trying to mark an override method in a derived class as obsolete

using
the ObsoleteAttribu te. The compiler, however is not picking up this
attribute and is not generating a warning or an error.

Sample code is below:

If I put the attribute on the static method DoSomething() in the main

class,
a warning/error is generated by the compiler.

If I put the attribute on the virtual instance method DoSomething() of
Class2, a warning/error is generated.

If I put the attribute on the override instance method DoSomething() of
Class3 ( which is derived from Class2), no warning or error is generated

by
the compiler.

Is this a bug, or is there some good reason for this? It certainly isn't
documented. Any ideas?
using System;
namespace ConsoleApplicat ion
{

class Class1
{
[STAThread]
static void Main(string[] args)
{
Class2 o = new Class2();
o.DoSomething() ;

o = new Class3();
o.DoSomething() ;

DoSomething();

Console.ReadLin e();
}

[Obsolete("Funct ion 'DoSomething()' is obsolete.", true )]
static void DoSomething()
{
Console.WriteLi ne( "I've done something.");
}
}

public class Class2
{
public Class2() {}

public virtual void DoSomething()
{
Console.WriteLi ne("Class2.DoSo mething()");
}
}

public class Class3 : Class2
{
public Class3(){}

[Obsolete("Use somethiing else instead.", true)]
public override void DoSomething()
{
Console.WriteLi ne("Class3.DoSo mething()");
}
}

}

Thanks,

Steve


Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
3344
by: Sridhar R | last post by:
Consider the code below, class Base(object): pass class Derived(object): def __new__(cls, *args, **kwds): # some_factory returns an instance of Base # and I have to derive from this instance!
0
1178
by: Jeff T. | last post by:
I cannot figure out how to get my class to serialize the way I want it to. Basically, I have generated a number of classes using the XSD utility. I then created a derived class from one of the generated classes to add a default constructor and helper methods. When this class serializes, it includes the xsi:type attribute of my derived type. Because my derived type is not in my schema, validation fails. How can I get my derived type to...
10
2390
by: Eric J. Smith | last post by:
I am attempting to add an Editor attribute to an IListSource derived class (specifically it's a typed DataSet). It appears that IListSource over rules any Editor attribute. Is there any way that I can get my Editor attribute to be used? Any help is greatly appreciated! Eric J. Smith
1
2083
by: Hans Bampel | last post by:
Hello group, i want to overwrite or manipulate a attribute of a property in a derived class. I use a attribute DBInfo via reflection on my properties to set the parameters in a SQL-statement dynamically. Now in a derived class (similar table in the DB), the name of the column a other one. what i have
9
1809
by: Larry Woods | last post by:
I have a method in my base class that I want ALL derived classes to use. But, I find that I can create a "Shadow" method in my derived class that "overrides" the method in my base class. Can't figure out what attribute to put on the base class method to prevent this. TIA, Larry Woods
3
5476
by: Michael Chambers | last post by:
Hi there, Is there a clean way to change a property or method's attribute in a derived class without redefining the property/method itself. That is, redefining the property/method in the derived class simply to change its attribute is ugly IMO. I assume you can do this and then simply defer to the base class version but it's unwieldy so I'm wondering if there's a better way. Thanks in advance.
0
2507
by: phobos7 | last post by:
Hi, I'm looking for a way to force a derived class to serialise/serialize as its base class. I was hoping that this was possible by just applying an attribute to the the sub class. public class BaseClass { }
0
977
by: shantanuaggarwal | last post by:
Hi all - would like to write a custom attribute, which, when applied to a constructor will make the compiler issue a warning. Just like the Obsolete Attribute, but with a name of my choosing. For e.g. instaed of generating message "This method is Obsolete", it should display a message "This method can not be called from here", because logically that method is not obsolete, but should not be called from that class. Can't override the...
2
697
by: she_prog | last post by:
I have a class derived from UserControl. I need to serialize an object of this class, but only some properties of it, as not all properties are serializable (some of the properties coming from UserControl are like that). When serializing, how could I ignore all the properties coming from the UserControl class? I know there is XmlIgnoreAttribute, but how could I set it to every property of UserControl, as it is not my class? Thank you...
0
10640
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10376
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10120
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9200
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6881
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5689
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4332
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3861
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.