Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / August 2006

Tip: Looking for answers? Try searching our database.

How do I find the dynamically type

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
tony - 17 Aug 2006 09:15 GMT
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
Marc Gravell - 17 Aug 2006 09:25 GMT
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
Jon Skeet [C# MVP] - 17 Aug 2006 19:53 GMT
> 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.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Marc Gravell - 18 Aug 2006 08:52 GMT
Agreed - as this would handle interfaces correctly as well. I wasn't
thinking straight.

Cheers

Marc
Patrick Philippot - 17 Aug 2006 09:25 GMT
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.

Signature

Patrick Philippot - Microsoft MVP
MainSoft Consulting Services
www.mainsoft.fr

Vadym Stetsyak - 17 Aug 2006 09:26 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.