2

I'm fairly new to C# and I tried my best to find an already answered question similar to mine but I just could not get the answers to relate to my question.

So, I have an "Instance"-class which is supposed to be a super class for all game objects. It has a read-only ClassName property defined as such:

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

And my reason for this question is that I want to have the following method which would return true if the current object is an instance of className, or if the object's class inherits from className.

    public bool IsA(string className)
    {
        //TODO
    }

This needs to be sort-of "recursive" so that if the case was: X : Class1 : Class2: Class4 then X.IsA(Class4) would return true

I've looked into methods like object.GetType() but I haven't figured out a solution.

Any ideas?

Elmub
  • 141
  • 1
  • 2
  • 11
  • This needs to be sort-of "recursive" so that if the case was: X : Class1 : Class2: Class4 then X.IsA(Class4) would return true – Elmub Oct 21 '16 at 02:17
  • Taken from the accepted answer in the duplicate thread: *`is` returns `true` if an instance is in the __inheritance tree__.* Relevant documentation: [is (C# Reference)](https://msdn.microsoft.com/en-us/library/scekt9xw.aspx). – sstan Oct 21 '16 at 02:19
  • Yes I just read that aswell but one problem still persists, my parameter is a string not a class – Elmub Oct 21 '16 at 02:21
  • 2
    You're entering dangerous waters if you are using a string here. Are you sure that you are stuck using a string? If you show us where the string comes from, we may be able to suggest a cleaner alternative. – sstan Oct 21 '16 at 02:24
  • https://gist.github.com/Elmuti/e09b066207eef0b551f0df6f56ce48f4 I wrote this based on that linked duplicate but I'm not sure if this will work – Elmub Oct 21 '16 at 02:27
  • Just fixed that gist, but I am not sure if it works for my needs – Elmub Oct 21 '16 at 02:31
  • I think the better question is why use a string to begin with? Your ClassName property / IsA method serve no purpose except to duplicate language features already available, and you're not even using the full type name so your re-implementation can't differentiate between the same class name in two different namespaces. If you need to print an object's type to the user / a log file, sure, go and get the name, but don't use the name programmatically, use the language features intended for solving exactly these problems. – PMV Oct 21 '16 at 03:56

1 Answers1

2

Just use the is or as statement. as will try to cast it to the target, and will be null if it fails (from memory).

Here's a quick linqpad demo (replace "yep".Dump()" with Console.Out.Write("yep"); for VS):

void Main()
{
    var  b = new bar();

    if (b is foo)
        "yep".Dump();
    else
        "nope".Dump();

    var b2 = new bar();
    var b3 = b2 as foo;
    // you can check if b3 is null, or if not, use it a foo.

}


class foo {}

class bar : foo {}
Noctis
  • 11,507
  • 3
  • 43
  • 82