Check if an object is a type object

F

Fredrik Strandberg

Hi!

I receive an object as a System.Object argument to a method. I need to
check if the object is a Type object or not (that is, not a specific
type, but if the object is a type object in general). How can I do
this? Is there a way of checking if my object inherits from the
System.Type class?

sub myMethod(obj as Object)

if ("obj is a type object")

...do my stuff...

I could not find any solution in any thread in the google groups. I
only find threads about how to determine the Type of an object, but I
need to determine if my object is a Type object!

As far as i understand, I am not able to use the Type.IsInstanceOfType
method since the Type class is abstract, i.e.

Type typeObj
typeObj.IsInstanceOfType(GetType(obj))

does not work.

Thank you for your time!

/Fredrik
 
P

parez

Hi

This might work



test(GetType(DataColumn))



Private Sub test(ByVal obj As Object)

MsgBox(obj.GetType.ToString)


End Sub
 
J

jvb

I would recommend

If TypeOf obj is <Type> then
..
ElseIf TypeOf obj is <Type> then.
..
End If
 
A

AlanT

If you have a routine

Sub fx(obj as object)
end sub

and call it with a type as a parameter

e.g

fx(GetType(Integer))

or,

dim str as string = ""
fx(str.GetType())


Then the incoming parameter will have a type of

System.RuntimeType


The nice way to check would be to compare with that type


if (obj.GetType() is GetType(System.RuntimeType)) then
....
end if

but System.RuntimeType is private and not accessible - that's what the
error message says ?

More clumsy, but will work, is to compare with the name

if (obj.GetType().ToString() = "System.RuntimeType") then
...
end if

will allow you to discern which of the incoming objects are types.


hth,
Alan.
 
F

Fredrik Strandberg

I may missinterpret your answers but,

parez - I cannot use a msgBox to determine manually if the obejct is a
type object, it has to be done automatically.

jvb - If I understand you correctly <Type> should be replaced with the
types I am looking for, but I am looking for all types. If the method
call to my method is:

myMethod(GetType(anyobject))

then I want to be able to determine that it is a type object that I
receive, in contrast to

Dim obj as MyClass

myMethod(obj)

where the obj (probably) is not a type object.

I do not however have any idea of which types the objects may be!

/Fredrik
 
F

Fabio

More clumsy, but will work, is to compare with the name

if (obj.GetType().ToString() = "System.RuntimeType") then
...
end if

will allow you to discern which of the incoming objects are types.

Never be esoteric in your code!

What about the

obj.GetType.Equals(GetType(obj))

?

or

obj.GetType().IsSubClassOf(GetType(obj)))
 
A

AlanT

-- In general it is probably better not too open an answer with a
criticism, especially when said criticism is dubious.

GetType() works on Types not objects - GetType(obj) doesn't compile


Unless I totally misunderstood the question, the issue is that objects
will be passed into the function

sub fx(obj as Object)


some of these will be actual objects - ints, strings, user defined
classes.
some of these will be Types

e.g.
fx(GetType(String))

dim i as integer
fx(i.GetType()) ' FYI, you this is how you find the type or an
object


The OP wanted to ascertain which of the incoming value were Types, not
the type of the incoming parameter.

My answer was (as I said) a touch clumsy, but did the job.

If there is another way to obtain the same result (one that actually
compiles), I would be interested in learning it.


Alan.
 
P

Phill W.

Fredrik Strandberg said:
Hi!

I receive an object as a System.Object argument to a method. I need to
check if the object is a Type object or not (that is, not a specific
type, but if the object is a type object in general).

You'd be better off looking into Overloading and writing methods for
/each/ object Type that you want to process but, if you /really/ want
to do it the Hard Way, try this:

Dim thing as New SomeReferenceType

If thing.GetType().IsSubclassOf( GetType( System.Object ) ) Then
' Reference Type
Else
' Value Type
End If

Can't think why you'd want to though ... ;-)

HTH,
Phill W.
 
M

Mattias Sjögren

but System.RuntimeType is private and not accessible - that's what the
error message says ?

More clumsy, but will work, is to compare with the name

if (obj.GetType().ToString() = "System.RuntimeType") then


What's wrong with

If TypeOf obj Is System.Type Then


Mattias
 
L

Larry Lard

Fredrik said:
Hi!

I receive an object as a System.Object argument to a method. I need to
check if the object is a Type object or not (that is, not a specific
type, but if the object is a type object in general). How can I do
this? Is there a way of checking if my object inherits from the
System.Type class?

The replies to this thread have astonished me.

Does this meet your requirements?

Public Sub whatisit(ByVal o As Object)
If TypeOf o Is Type Then
MsgBox("it's a type")
Else
MsgBox("it's not a type")
End If
End Sub
 
A

AlanT

Tried that first, unfortunately, it didn't work

If I have

VB.Net
----------
Private Sub fx(ByVal obj As Object)
Trace.WriteLine(obj.GetType.ToString() & " " & (obj.GetType Is
GetType(Type)).ToString)
End Sub

C#
----
private void fx(Object obj) {
Trace.WriteLine(obj.ToString() + " " + (obj.GetType() ==
typeof(System.Type)).ToString());
}

then

VB.Net
----------
fx(17)
fx(17.GetType())

C#
------
fx(17);
fx(17.GetType());


gives

System.Int32 False
System.RuntimeType False

Apparently, RuntimeType does not inherit from Type.


Alan.

%^&$% Cross-posted questions. Doubles my typing. ;)
 
A

AlanT

Update to previous reply

TypeOf (obj) //VB worked

where

typeof (System.Type) // C#

didn't.

thanks,
Alan.

What did I say about $%^^ cross postings ;)
 
M

Mattias Sjögren

Trace.WriteLine(obj.GetType.ToString() & " " & (obj.GetType Is
GetType(Type)).ToString)

I suggested you use the TypeOf .. Is operator. 'Is' by itself does
something different, it compares object identity.

Apparently, RuntimeType does not inherit from Type.

It sure does.


Mattias
 
F

Fredrik Strandberg

Thank you for all your help, google groups i amazing and I become happy
when there are people in the world willing to help each other!

Alan T and Larry Lard, both your descriptions described my problem
perfectly. Of all the suggestions, the following worked for me:

if ( obj.GetType().ToString() = "System.RuntimeType" )
if ( thing.GetType().IsSubclassOf( GetType (Type) ) )
if TypeOf obj Is Type

Phill W - about overloading, that was my first instinct. However, what
I am trying to do is build a wrapper class using Reflection to be able
to access private members of classes for testing purposes. I would like
to be able to invoke a private method with the same method call no
matter if the method is static or an instance method, and the objects
used with the wrapper class may be all known and unknown objects/types
in the universe =) :

providing an object for an instance method:

runMethod(myObject, "nameOfInstanceMethod")

and providing the class type for a static method:

runMethod(myType, "nameOfStaticMethod")

However if I would overload the runMethod as in:

runMethod(ByVal obj As Object)
runMethod(ByVal t As Type)

I think the first method would always be used, since System.Type
inherits from Object. Please correct me if I'm wrong =)

Anyway, the solutions provided by you fine folks work fine, so again -
thank you very much!

Have a brilliant day!

/Fredrik
 

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

Top