1

My problem is that MethodBase.GetCurrentMethod().DeclaringType.Name only works if called on an instance of a class. If called from a static function, it returns the topmost parent class' name regardless of how many times it has been sub-classed.

For example

public class ReflectionObject {

    public static string Name() {
        return MethodBase.GetCurrentMethod().DeclaringType.Name;
    }
}
public class DerivedObject : ReflectionObject {
}

Calling DerivedObject.Name() returns ReflectionObject and not DerivedObject like I need it to. Any ideas?

Lorenzo Ang
  • 1,202
  • 3
  • 12
  • 35
  • 3
    It is a `static` method. These are not inherited in C#, so it makes perfect sense that the `DeclaringType` would be the base class. At any rate, this sounds like [an XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem); why do you need the class's name as a string? What are you actually trying to do? – Cody Gray - on strike Jun 03 '17 at 14:39
  • What is your use case here? If you are calling `DerivedObject.Name()` then you already have `DerivedObject`... It isn't very clear what your requirements here are... – Chris Jun 03 '17 at 14:40

2 Answers2

1

Static inheritance doesn't work like inheritance of normal methods. The behavior you observe happens instead (it acts as a method of the parent class, not of the child). User Daniel Earwicker put it really well in this answer. In short, static methods are tied to their declaring type.

Now, regarding your specific problem, there isn't a way to make what you're trying to do work. There are however a few alternative ways to achieve your desired result. The simplest is to just use a property instead of a method.

public class ReflectionObject
{
    public string Name
    {
        get {
            return this.GetType().Name;
        }
    }
}

Then if you had a method such as this in your child class:

public class DerivedObject : ReflectionObject
{
    public string MyName()
    {
        return this.Name;
    }
}

..calling something like new DerivedObject().MyName() would return "DerivedObject" as is your desired result. Using this in the property instead of GetType() is also an option:

MethodBase.GetCurrentMethod().DeclaringType.Name;

Of course, since you don't mention your use case I cannot know of any limitations you may have. Generally, this is the easiest way to get the name of a child class.

stelioslogothetis
  • 9,371
  • 3
  • 28
  • 53
  • "Generally, this is the easiest way to get the name of a child class" - I'm not sure I see the point if you have to use the name of the child class to create a copy of it just to ask it its name... Its a flaw in the original question though really rather than your answer. It does feel like it makes this answer a little trivial given this and the fact that your answer to "How do I do X in a static method" seems to be "Don't use a static method" – Chris Jun 03 '17 at 15:12
  • I merely used `new DerivedObject().MyName()` as an example. I doubt OP would actually use this. As for the value of the answer in general, this is one of those cases where the only way to achieve OP's desired result is via a workaround. The design of C# prohibits static methods being inherited, so what OP is trying to do is either impossible, or requires insane reflection trickery. Without any context from OP, this is all I can tell them. – stelioslogothetis Jun 03 '17 at 15:17
  • Bummer, I had a feeling this would be the case but I was hoping there was a work around somewhere. The thing is, I'm going to have a lot of classes - upwards of 50 and I wanted to find a way to implement it all at once for all of them to save myself some time. – Lorenzo Ang Jun 03 '17 at 15:46
  • @Dilisqq I'm still very confused as to why you want this. If you are calling a static method then you have the class name already. What exactly are you trying to implement? I really feel we can probably help you if you tell us what your actual problem is... – Chris Jun 03 '17 at 19:51
1

It's not possible as the static method is bound to the class you're declaring it in. You can't mark it virtual or override it either.

If you want the name, you could treat the base static method as a helper function and pass in the name from the deriving class.

arviman
  • 5,087
  • 41
  • 48
  • Nah, the idea was to do it once and have all my classes -upwards of 50 - just inherit it. I had a feeling it wouldn't be possible but thanks! – Lorenzo Ang Jun 03 '17 at 15:48