2

I want to change the type with which I am comparing in an "is" statement at runtime, which I believe is not possible with "is" if i understand this Q&A right But I don't fully understand the answer given there,

could somebody please give an example on how to make a working type comparison with a changeable type ?

Similar to what I have tried here : //does not work (at least not with my c# ;-) )

    public static Type T;

    public class A { }
    public class B { }

    public static void Main(string[] args)
    {

        A AObject = new A();

        T = typeof(A);

        Console.WriteLine(AObject is T); // schould print true if it worked

        T = typeof(B);

        Console.WriteLine(AObject is T); // should print false if it worked

        Console.Read();
    }

The Question linked as Reason for closing this one is not considered a good on topic question I agree with that, I have a simple question for a topic that schould be simple and I want a simple answer and not a Tips and trics guide, that is not even considered fitting for this Q&A Format.

And I got that simple answer Thanks verry much !

majorTom
  • 57
  • 6

3 Answers3

2

T.UnderlyingSystemType == typeof(A)

is another way to compare types

MuKa
  • 165
  • 2
  • 2
  • 13
1

This can be done by taking the object's type and comparing it against a string value. You do not need the Type member.

using System;

namespace ConsoleApplication1
{
    class Program
    {
        public static Type T; //Not needed

        public class TypeA
        {
            public int testProp { get; set; }

            public string testPropTwo { get; set; }
        }

        public class TypeB
        {
            public decimal testProp { get; set; }

            public bool testPropTwo { get; set; }
        }

        static void Main(string[] args)
        {
            TypeA typeA = new TypeA();
            TypeB typeB = new TypeB();

            //Read type of user input. Mimicking dynamic value
            var inputType = Console.ReadLine();

            //Comparison with types.
            Console.WriteLine(typeA.GetType().Name == inputType);

            Console.WriteLine(typeB.GetType().Name == inputType);

            Console.ReadKey();
        }
    }
}
  • well but how does *that* answer the question now? it neither addresses the intricacies of the `is` operator vs. the `typeof(t)==T` approach, nor does it replicate the almost-MVCE of OP – Cee McSharpface Apr 27 '18 at 11:28
  • 1
    Here is the point of the initial question - "please give an example on how to make a working type comparison with a changeable type". Hence the answer. –  Apr 27 '18 at 11:29
  • ok, fair point; still the use of string representations of type names adds yet another layer to the puzzle IMO – Cee McSharpface Apr 27 '18 at 11:30
  • 1
    What I saw was a fairly standard requirement. Often in projects we have to compare types at runtime, hence I presumed (maybe wrongly) that it is a straightforward concern. –  Apr 27 '18 at 11:32
  • It answered the question percisely thanks verry much! – majorTom Apr 27 '18 at 11:35
  • Glad that is the case. Cheers! –  Apr 27 '18 at 11:37
0

In Essence what i was loking for is something like this :

    public static bool isObjOfType(Object OBJ, Type Type)
    {
        return OBJ.GetType().Name == Type.Name;
    }

    public static bool isObjOfType(Object OBJ, string Type)
    {
        return OBJ.GetType().Name == Type;
    }
majorTom
  • 57
  • 6