Obsolete attribute in a derived class

S

Steve James

I am trying to mark an override method in a derived class as obsolete using
the ObsoleteAttribute. 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 ConsoleApplication
{

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

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

DoSomething();

Console.ReadLine();
}

[Obsolete("Function 'DoSomething()' is obsolete.", true )]
static void DoSomething()
{
Console.WriteLine( "I've done something.");
}
}

public class Class2
{
public Class2() {}

public virtual void DoSomething()
{
Console.WriteLine("Class2.DoSomething()");
}
}

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

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

}

Thanks,

Steve
 
P

Philip Rieck

If you look into the il generated, you'll notice the line :
callvirt instance void
ConsoleApplication1.ConsoleApplication.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.DoSomething is virtual, the
instance call to Classxxx.DoSomething() will always be a callvirt to
Class2.DoSomething, 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.DoSomething() is not
actually called directly.
 
S

Steve James

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 said:
If you look into the il generated, you'll notice the line :
callvirt instance void
ConsoleApplication1.ConsoleApplication.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.DoSomething is virtual, the
instance call to Classxxx.DoSomething() will always be a callvirt to
Class2.DoSomething, 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.DoSomething() is not
actually called directly.




Steve James said:
I am trying to mark an override method in a derived class as obsolete using
the ObsoleteAttribute. 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 ConsoleApplication
{

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

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

DoSomething();

Console.ReadLine();
}

[Obsolete("Function 'DoSomething()' is obsolete.", true )]
static void DoSomething()
{
Console.WriteLine( "I've done something.");
}
}

public class Class2
{
public Class2() {}

public virtual void DoSomething()
{
Console.WriteLine("Class2.DoSomething()");
}
}

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

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

}

Thanks,

Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top