inheritance and reflection

  • Thread starter Thread starter Michael Dybevick
  • Start date Start date
M

Michael Dybevick

In the Can You Even Do This department:

What do I substitute for the YadaYada in order to get the
Debug.WriteLine statement to write the string "LayerEHIS" ?


using System;
using System.Diagnostics;

namespace Layer
{
class classMain
{
static void Main(string[] args)
{
Debug.WriteLine(LayerEHIS.NAME);
}
}


abstract class Layer
{
internal static string NAME
{
get
{
return System.Reflection.YadaYada;
}
}
}

class LayerEHIS : Layer
{
}
}
 
Michael Dybevick said:
In the Can You Even Do This department:

What do I substitute for the YadaYada in order to get the
Debug.WriteLine statement to write the string "LayerEHIS" ?
abstract class Layer
{
internal static string NAME
{
get
{
return this.GetType().Name();
}
}
}


David
 
Michael,
In the Can You Even Do This department:

What do I substitute for the YadaYada in order to get the
Debug.WriteLine statement to write the string "LayerEHIS" ?

If NAME had been an instance property and you called it on a LayerEHIS
instance, you would use

return this.GetType().Name;

But since NAME is static, there's no relation between it and the
LayerEHIS class.



Mattias
 
But since NAME is static, there's no relation between it and the
LayerEHIS class.

Right. I know that code is laid down before any derived classes are created,
but is there no mechanism for the runtime to be made aware that the call is
coming via a reference from a derived class? Or does any trace of that
dissappear after the compiler gets through figuring things out?
 
Michael Dybevick said:
Right. I know that code is laid down before any derived classes are created,
but is there no mechanism for the runtime to be made aware that the call is
coming via a reference from a derived class? Or does any trace of that
dissappear after the compiler gets through figuring things out?

All trace is removed. For example, compile:

using System;

class Test
{
static void Main()
{
Foo.Main();
}
}

class Foo : Test
{
}

The IL for Main ends up as:

..method private hidebysig static void Main() cil managed
{
.entrypoint
// Code size 6 (0x6)
.maxstack 0
IL_0000: call void Test::Main()
IL_0005: ret
} // end of method Test::Main
 
Back
Top