Solved

TYPEs - GetType, typeof(), etc

Posted on 2007-04-11
3
791 Views
Last Modified: 2012-08-14
Please explain to me about Types and the methodss GetType(something), typeof(something) and myobject.GetType()

They seems to all do subtley different things and I'm never sure how to use them.

For example, when serializing I need a type to create a new serializer:
dim foo as new XmlSerialzier(System.Data.DataView)

But what if I have an object reference and I want to pull its type out.... how do I do that?  The various combinatuion of the above crash:
dim foo as new XmlSerialzier(   GetType(MyObject) )  ' does not compile - "Type MyObject not defined"
dim foo as new XmlSerialzier(   MyObject.GetType() )  ' compiles but crashes  
dim foo as new XmlSerialzier(  typeof( MyObject) )  ' does not compile - " 'Is' expected"

Please explain these different methods to me and tell me how to pull out and use the type of an object reference.


Thanks



0
Comment
Question by:jasww
  • 2
3 Comments
 
LVL 3

Expert Comment

by:JipFromParis
ID: 18889591
You write : dim foo as new XmlSerialzier(   GetType(MyObject) )  ' does not compile - "Type MyObject not defined"
You shoudl write : dim foo as new XmlSerializer(Type.GetType(MyObject) ) ' GetType with one parameter is a static method of the System.Type class and thus should be prefixed by the class name.

you write : dim foo as new XmlSerialzier(   MyObject.GetType() )  ' compiles but crashes  
OK : How does it crash ? Any stack trace and exception message ?

You write : dim foo as new XmlSerialzier(  typeof( MyObject) )  ' does not compile - " 'Is' expected"
You should write something else because typeof Visual Basic operator is completely different from the same operator in C# or C/C++. The VB syntax is :
result = TypeOf objectexpression Is typename
where result is a boolean value which is the result of evaluating 'objectexpression', then getting the real type of the result and comparing it with the type which name is given by 'typename' in order to assert if both types are compatible.

Pulling out the type of an object instance is as easy as writing : myInstance.GetType()
0
 

Author Comment

by:jasww
ID: 18889647
The reason that
 dim foo as new XmlSerialzier(   MyObject.GetType() )  ' compiles but crashes  
crashes is because of a problem with the class I'm trying to serialise.  The Data.DataView class seems to dislike being serilaised and crashes with:

"You must implement the Add(System.Data.DataRowView) method on System.Data.DataView because it inherits from ICollection."
I got it working by serialising the parent dataset instead.

So my misunderstanding was not as great as I first thought.


However, the question still remains - kindly explain Type.GetType(sometype)  or GetType(sometype)   versus myobject.GetType(), and how these equate to C#'s typeof()


Thanks
0
 
LVL 3

Accepted Solution

by:
JipFromParis earned 125 total points
ID: 18889769
You've got several options all of which are returning you an instance of the System.Type class describing a class, a structure, an enumeration, a delegate ... :

1a°) Get the type description of a type by type name. Use Type.GetType(typeName) where you provide the type name for example :
Type.GetType("System.Object") ' Notice the argument which is a string.
1b°) Do it in C# and use the typeof operator such as : typeof(System.Object)  ' Notice we provide the type name directly. The typeof operator is resolved by the compiler. This implies you must provide a valid type name at compilation. On the other hand the Type.GetType() method will let you compute its string argument at runtime which might be helpfull in certain circumstances.

2°) Get the type description of an object. User theObject.GetType() ' where theObject is the instance you want to retrieve the type description.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now