1

I need to compare types in C# flexibly, for the purpose of testing types hierarchical relationships. My first try is

public class ParentInfo<PT> {  // PT = parent type expected
    
    public static bool hasParent(LDBRootStructs item) {
        var parent = item.parent;
        var rightParent = parent is PT;  // << this is the type test I want

This is fine but it means instantiating the generic parameter PT to a known class at compile time. I need it as a variable so I tried this instead

public static class ParentInfo2 {  // PT = parent type expected
    
    public static bool hasParent2(LDBRootStructs item,
                                      Type PT) {
        var parent = item.parent;
        var rightParent = parent is PT; <<< error here

but it complained “ Cannot implicitly convert type 'System.Type' to 'LDB.LDBRootStructs' ” and additionally I can't pass in the requested type as a parameter.

I can work around this by adding a test to each of my classes (each class has myOwnType virtual func which returns an int that is fixed for each class, just compare these ints), but is there an easier way? I’ve been looking at GetType and related but it’s unclear.

I need to do this to create an interpreted DSL so it has to be flexible. I’ve looked for previous answers to this (this is SO, they must exist!) but I don’t know what to look for.

-- edit, realised just a way to get a unique id (string, int, whatever) from a class would likely suffice.

1
  • re "edit, realised just a way to get a unique id (string, int, whatever) from a class would likely suffice." - make them all implement an interface with an Id property
    – Jamiec
    Dec 7, 2020 at 15:15

3 Answers 3

4

You're trying to compare an instance of a class to an instance of Type - that wont work (or at least won't do what you expect).

Compare the type of parent to the type PT

var rightParent = parent.GetType() == PT; 

This will only match exactly. For a hierarchy/inheritance chain of types you could also use

var rightParent = PT.IsAssignableFrom(parent.GetType()); 
3
  • This looks like the right direction but I still have the problem of passing in PT as a (normal) parameter. I can recieve it as "Type PT" in the param list above, but how do I pass it in as an argument when I call it: "var ok1 = ParentInfo.hasParent(this, TVDeclarationStatement..." complains "'TVDeclarationStatement' is a type, which is not valid in the given context" Dec 7, 2020 at 16:22
  • @user3779002 typeof(TVDeclarationStatement) is what you're after I think
    – Jamiec
    Dec 7, 2020 at 16:23
  • Aaaaand liftoff! Thanks, answer accepted. That will save me much wasted time. Dec 7, 2020 at 16:45
1

The reflection-based equivalent to

bool IsOfType<T>(object o)
{
    return o is T;
}

uses Type.IsAssignableFrom

bool IsOfType(object o, Type t)
{
    return t.IsAssignableFrom(o.GetType());
}
1
  • Other answer is more complete so I took that, but thank you anyway Dec 7, 2020 at 16:46
-1

This should work

        var foo = new Foo();
        Type bar = typeof(Foo);

        if (foo.GetType().Equals(bar))
        {

        }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.