I need to access a const class value in an instance of that class, without knowing the type of the class, and also be able to access it on the class itself. How can I do this?
Example of what I want to achieve:
public abstract class A { }
public class B : A
{
public const int X = 50;
}
...
A b = new B();
b.X ???
This is very incomplete, but what I want to achieve is access B's X constant from a variable of type A, through polymorphism (if that makes sense).
This is one way I tried to achieve this:
public abstract class A
{
public abstract int X { get; }
}
public class B : A
{
public const int X = 50;
public override int X { get { return B.X; } } // or return 50;
}
The problem with this though is that C# won't let this compile because of the duplicate definition of X in B. So how should I do this? Is there a better way? Or my only option is to name them differently?