473,748 Members | 5,232 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Check if an object is a type object

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.IsInstance OfType
method since the Type class is abstract, i.e.

Type typeObj
typeObj.IsInsta nceOfType(GetTy pe(obj))

does not work.

Thank you for your time!

/Fredrik

Feb 28 '06
13 25869


Tried that first, unfortunately, it didn't work

If I have

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

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

then

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

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

System.Int32 False
System.RuntimeT ype False

Apparently, RuntimeType does not inherit from Type.
Alan.

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

Feb 28 '06 #11

Update to previous reply

TypeOf (obj) //VB worked

where

typeof (System.Type) // C#

didn't.

thanks,
Alan.

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

Feb 28 '06 #12
> Trace.WriteLine (obj.GetType.To String() & " " & (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

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Feb 28 '06 #13
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().T oString() = "System.Runtime Type" )
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(myObj ect, "nameOfInstance Method")

and providing the class type for a static method:

runMethod(myTyp e, "nameOfStaticMe thod")

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

Mar 1 '06 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2011
by: Torsten Mohr | last post by:
Hi, i write a set of extension modules for python in C. I also use some new types that i defined there. At the moment i write some methods that expect some of these types as parameters. I now want to check that the right types were given as parameters. How can i do that?
6
9531
by: Damon | last post by:
Hi, I created a template class and stored its instances in a map object that is like std::map<std::string, void* >. But i just made a nasty discovery that if i fail to use a C-style cast properly with the void pointer to return it to the proper class it belongs to, I get very bad and random segmentation fault. So question to all the experts, can I check the type of a void pointer or use some guaranteed way to elicit an exception...
1
2460
by: Ping | last post by:
Hi, i am trying to : dim db As database and i have Access 2002. The object type "database" is not in the list and error message is "type mismatching " when run. i am wondering if the "database" object type is replaced by some other object or i did not install the office XP correctly.
2
5184
by: MFRASER | last post by:
How can I tell what object type is dropped onto my control? Here is a snippet of code. private void lvwTasks_DragDrop(object sender, System.Windows.Forms.DragEventArgs e) { //I want to know if e.Data is of type MyClass1 or MyClass2
2
2133
by: Just D. | last post by:
All, Do we have a simple way to Create an object on the fly knowing just an object type? The usual design-time way is to write a code something like this: CObjectType obj = new CObjectType(); That's simple. But to create any object knowing its object type on the fly is looking like a problem. I'll try to explain the idea.
5
1843
by: Don | last post by:
I have an array of System.type and I need to go through the array and perform different logic depending on the type stored in the array. I want to do: if (typeof typeCollection(i) is String) then 'some logic elseif (typeof typeCollection(i) is int32) then 'some logic .....
6
2205
by: tommaso.gastaldi | last post by:
Hi, does anybody know a speedy analog of IsNumeric() to check for strings/chars. I would like to check if an Object can be treated as a string before using a Cstr(), clearly avoiding the time and resource consuming Try... Catch, which in iterative processing is totally unacceptable. -tom
5
24253
bvdet
by: bvdet | last post by:
Python 2.3 Windows XP We have a builtin module 'point'. I am trying to determine a better way to check the type of a list of 'point' objects. A point object is created:from point import Point pt1 = Point(x_coord, y_coord, z_coord) print type(pt1) <type 'point'> print dir(pt1) Original code: def chk_type(p_list): ret_list =
6
1977
by: Deckarep | last post by:
I want to be able to pass in a function a string say: "TextBox" Then I need a way to convert that string representation into a Type object so i can search through some controls and check their type. Then I can do my check: If ( control is type) { //continue } I'm doing this inside of a recursive function which goes through the ControlTree.
0
8991
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8831
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9548
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9325
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
4607
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4876
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3315
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2787
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2215
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.