jasww
asked on
TYPEs - GetType, typeof(), etc
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
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.
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
ASKER
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.DataRowVie w) 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
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.DataRowVie
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You shoudl write : dim foo as new XmlSerializer(Type.GetType
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()