Check the type of a class

I have the following C# classes:

public class Reply<T> { } public class Ok<T> : Reply<T> { } public class BadRequest<T> : Reply<T> { }

And on a method which receives a Reply I need to check it its type is Ok or BadRequest or ... Something like:

public static String Evaluate(Reply<T> reply) { switch (typeof(reply)) { case typeof(Ok<T>): // Do something break; // Other cases } }

But I get the error

The type or namespace name 'reply' could not be found (are you missing a using directive or an assembly reference?)

Any idea how to test the type of reply?

-------------Problems Reply------------

Well, typeof() only works on types (as in typeof(int)), not variables, so you would need



But then you'll find that case expressions require literal values, so you'll need to convert to an if-else block:

public static String Evaluate<T>(Reply<T> reply) {
if(reply.GetType() == typeof(Ok<T>)) {
// Do something
else {
// Other cases


if(reply is Ok<T>) {
// Do something
else {
// Other cases

reply.GetType() is what you're looking for

You can use typeof which is an operator that returns the System.Type of an Object

Category:c# Views:1 Time:2018-06-11

Related post

  • Can we use match to check the type of a class 2011-03-03

    I'm new to scala, and I'm learning the match keyword now. I wanna know if we can use the keyword match to check the type of a class. My code is: object Main { def main(args: Array[String]) { val x = "AA" checkType(x) } def checkType(cls: AnyRef) { cl

  • what is the best way to check the type of base class pointer? 2009-01-19

    I want to know the runtime type of a base class pointer, I know you can use dynamic_cast. is there any better way? --------------Solutions------------- dynamic_cast will only confirm your guess, and even that is not perfect. If C inherits from B whic

  • Should I check the types of constructor arguments (and at other places too)? 2009-03-02

    Python discourages checking the types. But in many cases this may be useful: Checking constructor arguments. e.g. checking foe Boolean, string, dict etc. If I don't and set the object's members to the arguments it will cause problems later. Checking

  • iphone how to check the type of an Object? 2009-09-02

    HI , i am new to iPhone. i want to check the type of an Object. how i will do that ... the scenerio is i m getting an object . if that object is of type A then do something .. if it is of type B then do something .. currently the type of the object i

  • .NET: Determine the type of "this" class in its static method 2010-01-17

    In a non-static method I could use this.GetType() and it would return the Type. How can I get the same Type in a static method? Of course, I can't just write typeof(ThisTypeName) because ThisTypeName is known only in runtime. Thanks! --------------So

  • C++ checking the type of reference 2010-01-18

    Is it bad design to check if an object is of a particular type by having some sort of ID data member in it? class A { private: bool isStub; public: A(bool isStubVal):isStub(isStubVal){} bool isStub(){return isStub;} }; class A1:public A { public: A1(

  • How to check the type of object in ArrayList 2010-03-15

    Is there a way to get the type of object in the arraylist? I need to make an IF statment as the following (in C#): if(object is int) //code else //code thanks --------------Solutions------------- you can use the normal GetType() and typeof() if( obj.

  • different ways of checking the type of an object in java? 2010-04-30

    I have an object obj and a class name MyClass, i can check whether obj is of the type MyClass using either instanceof or i can say obj.getClass().equals("MyClass"). So i want to know are there any other ways of checking the type of an object. -------

  • How do I check the type of a child inside a div? 2010-07-15

    I want to check the type of an element which is placed inside a div. I'm not able to reference the child element. I have tried using the following: $("#"+styleTarget).siblings(0).get(0)tagName Where styleTarget is a variable holding the id of the par

  • Django - Checking the type of Multi-table inheritence Querysets 2011-11-30

    I'm trying to hold a kind of table of contents structure in my database. Simplified example: class Section (models.Model): title = models.CharField(max_length=80) order = models.IntegerField() class SectionClickable(Section): link = models.

  • How to check the type of a Parameter in an Expression Tree (similar to 'is' keyword) 2010-06-08

    How do I check the type of a Parameter in an Expression Tree (and get the Expression Tree equivalent of a bool if it the right type)? If it were normal code, I would do this: if(myObj is int) I see there is a Expression.Convert method but this conver

  • Is there a better way of checking the type in a datarow? 2011-02-17

    I have to check whether the first cell in my datarow is a datetime object.I'm doing the following for it.Could you please let me know whether there is a better way to do it? public bool ShouldProcess(DataRow theRow) { try { Convert.ToDateTime(theRow[

  • How to get the type of derived class at base class without generics? 2011-03-01

    My base class use reflection on derived classes to provide some functionality to them. Currently I do it this way: abstract class BaseClass<T> { string GetClassString() { // Iterate through derived class, and play with it's properties // So I n

  • Check the type of user input against known type 2012-02-01

    I have a situation. I am using wx.textctrl where user needs only to enter a number (positive integers only). I want to check what the user has entered . If he has entered a string , i want to do something like this: if type(user_input) == str: # do s

  • Checking the type of a link in Sitecore 2012-02-06

    I have a 'General Link' field in one of my pages in Sitecore. This field basically specifies the link of a video file which is used to play a video. Now, what I am trying to do is, check if the type of this link, whether the user has uploaded a link

  • How do you check the type of a local variable? 2012-02-09

    Simple question. Is it possible to check the type of a variable that is only alive within a function? For example: main = do x <- something How can I check the type of x? I can't do :type x in ghci because x is not global. --------------Solutions-

  • How to check the types of JSON values? 2016-07-19

    I would like to ask if someone knows how to check the type of a JSON value in Java? For example if we have the following JSON file: { "Name": { "First": "Job", "Last": "MOUN" }, "phoneNumbers": [

  • .NET: How to check the type within a generic typed class? 2008-10-24

    How do I get the type of a generic typed class within the class? An example: I build a generic typed collection implementing ICollection< T>. Within I have methods like public void Add(T item){ ... } public void Add(IEnumerable<T> enumIte

  • How to check the type name of an object in derived classes? 2010-05-05

    This is my code: class Base { /* something */ }; class Derived : public Base { /* something */ }; vector<Base*> v; // somebody else initializes it, somewhere int counter = 0; for (vector<Base*>::iterator i=v.begin(); i!=v.end(); ++i) { if

  • What is the best (idiomatic) way to check the type of a Python variable? 2008-12-18

    This question already has an answer here: Differences between isinstance() and type() in python 5 answers I need to know if a variable in Python is a string or a dict. Is there anything wrong with the following code? if type(x) == type(str()): do_som

  • C++ polymorphism: Checking data type of sub class 2010-11-05

    Possible Duplicate: Finding the type of an object in C++ Hello, I am sorry if it's a duplicate but I was not able to find answer to my question here. Let's assume we have following class structure in c++: class CPolygon { protected: int width, height

  • Is it good form to have an __init__ method that checks the type of its input? 2010-11-17

    I have a class that wants to be initialized from a few possible inputs. However a combination of no function overloading and my relative inexperience with the language makes me unsure of how to proceed. Any advice? --------------Solutions------------

  • Verify the type of custom class in function 2011-05-31

    In C#,I have a public function that can pass a List parameter, with T is a custom class. The function can pass with different T class. The problem that how to verify the type of T in every case? public static List<T> ConvertData(List<T> o

  • How to establish the type of a class type in Java, without instantiated objects? 2011-06-05

    Suppose I have an array of class values in Java and I want to iterate through the array to find at which position is located a Double. I tried the following: public class ClassTypes { public static void main(String[] args) { Class []types = {String.c

Copyright (C), All Rights Reserved.

processed in 0.096 (s). 11 q(s)