I inherit a class from a base class “MyLog”.
If I call “Write” within the inherited class, I want it to prefix any messages with the name of the inherited class.
As this application needs to be fast, I need some way of avoiding reflection during normal program operation (reflection during startup is fine).
Here is some code to illustrate the point:
class MyClassA : MyLog
{
w("MyMessage"); // Prints "MyClassA: MyMessage"
}
class MyClassB : MyLog
{
w("MyMessage"); // Prints "MyClassB: MyMessage"
}
class MyLog
{
string nameOfInheritedClass;
MyLog()
{
nameOfInheritedClass = ?????????????
}
w(string message)
{
Console.Write(nameOfInheritedClass, message);
}
}
I suspect you want:
GetType()always returns the actual type of the object it’s called on.