How do I find the dynamically type

  • Thread starter Thread starter tony
  • Start date Start date
T

tony

Hello!!

Assume I have this method
public void foo(object obj);
and I want to find out what type this obj is.
How do I do that?
I can look in the code but assume the code is very complicated.

//Tony
 
assuming obj isn't null, then obj.GetType() will return you the Type object,
which gives you lots of info.
If possible, avoid == (or ReferenceEquals) on types (unless you really mean
it) - this is often a bug, as you should also check .IsSubclassOf().

Any use?

Marc
 
Hi,
Assume I have this method
public void foo(object obj);
and I want to find out what type this obj is.
How do I do that?

Any object in .Net is derived from the root Object class. And Object has a
method named GetType that will do exactly what you want.
 
Hello, tony!

t> Assume I have this method
t> public void foo(object obj);
t> and I want to find out what type this obj is.
t> How do I do that?
t> I can look in the code but assume the code is very complicated.

you can call obj.GetType().
--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
 
Marc Gravell said:
assuming obj isn't null, then obj.GetType() will return you the Type object,
which gives you lots of info.
If possible, avoid == (or ReferenceEquals) on types (unless you really mean
it) - this is often a bug, as you should also check .IsSubclassOf().

.... or IsAssignableFrom.
 
Agreed - as this would handle interfaces correctly as well. I wasn't
thinking straight.

Cheers

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top