15610

Getting the type of a “Type” in C# reflection

Question:

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)

Answer1:

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

Check out... <a href="http://msdn.microsoft.com/en-us/library/system.type_members.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/system.type_members.aspx</a>

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...

<a href="https://stackoverflow.com/questions/1661913/typeofsystem-enum-isclass-false" rel="nofollow">typeof(System.Enum).IsClass == false</a>

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

Answer2:

There are a slew of properties off the Type class.

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

etc.

<a href="http://msdn.microsoft.com/en-us/library/system.type_properties.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/system.type_properties.aspx</a>

Answer3:

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

Answer4:

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)) { //... }

Answer5:

<a href="http://msdn.microsoft.com/en-us/library/system.type.isclass.aspx" rel="nofollow">Type.IsClass Property</a>?

Recommend

  • SDL_ttf cannot find “SDL.h”, but main.cpp can
  • Whats the correct way to save a file when using the Content sheme?
  • Why does json.loads() work on some strings? [duplicate]
  • Magento Not Showing Products to Users who are Not Logged in
  • What's the best way for a formula to provide attribute defaults?
  • How do I enable HiDPI (Retina) support in a Qt4 OpenGL application?
  • Accumulating while in recursion/backtracking
  • Explain what is difference between without whitespace in scanf and with whitespace in scanf?
  • Qt4: adjust which widget get focused on start
  • find distinct embedded documents from the document using Spring Data Mongo?
  • Refresh less css which has other imported less files without page load
  • Create my own classList object when the browser does not implement it itself
  • Xamarin.Forms bind Height of Grid to Width of Button
  • “RepeatForUnit” item missing in Calendar entry?
  • Ruby on Rails - Settting up Reviews functionality
  • NHibernate add unmapped column in interceptor
  • How to make a dependent dropdown in codeigniter
  • Immutability and synchronization in Java
  • How to sort by Lucene.Net field and ignore common stop words such as 'a' and 'the
  • How to search for a method or variable name within android project?
  • Why am I getting an Argument exception when creating event handler dynamically?
  • Smarter Removing Unnecessary WhiteSpace CSV
  • PowerShell script to pass SecureString to Plink as account and sudo passwords
  • Load php page results into div with ajax with link?
  • ASP.NET GridView throws: The version of SQL Server in use does not support datatype 'date'
  • Admob in ListView not clickable
  • How to add html image in to velocity template file to send email?
  • How to use AJAX to upload large CSV file? [closed]
  • Typeahead.js does give me suggestions but doesn't select them
  • How to split wav file into two or more parts using c#
  • Content-Type alternative in MQTT
  • Terminal run dalvikvm with am.jar
  • How to handle div that is created dynamically in a table
  • How to encrypt Connectionstring written in web.config from codebehind?
  • Make checkout phone field optional for specific countries in WooCommerce
  • Call Microservice from another Microservice within Docker
  • ReferenceError: TextEncoder is not defined