tags:

views:

243

answers:

5

This is harder to find in the docs than I imagined. Anyway, I have some instances of Type. How can I find out if they represent classes, methods, interfaces, etc?

class Bla { ... }
typeof(Bla).GetKindOrWhatever() // need to get something like "Class"

(I'm using Mono on Linux but that shouldn't affect the question, I'm making portable code)

+8  A: 

Type.IsClass might help here. Also Type.IsInterface.

Check out... http://msdn.microsoft.com/en-us/library/system.type%5Fmembers.aspx

There are quite a few "IsXxxx" properties on Type. Hopefully these will do what you want.

By the way, you should check out other questions on this subject on SO, including this one...

http://stackoverflow.com/questions/1661913/typeofsystem-enum-isclass-false

... if the types you're going to be checking are enum types, as there are some strange (but predictable) results.

Martin Peck
using that now. what i can't find is an equivalent to type.IsStruct :s
Bart van Heukelom
IsValueType is the closest you're going to get to that. struct is a C# language thing, not a .NET CLR thing. All structs are Value Types. Keep in mind the thing about Enums that I mentioned above though
Martin Peck
Are there any other value types that could appear in Assembly.getExportedMembers()? I doubt primitives will show up there, and those two are the only value types I can think of right now.
Bart van Heukelom
Oh and by the way, just out of curiosity, how are structs then implemented in the CLR?
Bart van Heukelom
+8  A: 

There are a slew of properties off the Type class.

typeof(Bla).IsClass
typeof(Bla).IsInterface

etc.

http://msdn.microsoft.com/en-us/library/system.type_properties.aspx

mkedobbs
A: 

Type.IsClass Property?

fretje
+1  A: 

The Type class have some properties that are named IsXXX.
For example it has a IsEnum, IsClass, IsInterface.
If I understand correctly your question this is what you need

Limo Wan Kenobi
+1  A: 

As others have mentioned, the Type class has various properties that you can use if you just need to know whether your type is a class, interface, delegate, etc.

If you have a Type object and you want to know if it is a specific type, then I recommend using the IsAssignableFrom method found on the Type class:

        Type objectType = obj.GetType();
        Type interfaceType = typeof(IExample);

        if (interfaceType.IsAssignableFrom(objectType))
        {
            //...
        }
Dr. Wily's Apprentice