|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Check if an object is a type objectI 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 Hi
This might work test(GetType(DataColumn)) Private Sub test(ByVal obj As Object) MsgBox(obj.GetType.ToString) End Sub I would recommend
If TypeOf obj is <Type> then .. ElseIf TypeOf obj is <Type> then. .. End If 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 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. "AlanT" <alanto***@users.com> ha scritto nel messaggio Never be esoteric in your code!news:1141140530.819570.291990@p10g2000cwp.googlegroups.com... > 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. What about the obj.GetType.Equals(GetType(obj)) ? or obj.GetType().IsSubClassOf(GetType(obj))) -- 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. >but System.RuntimeType is private and not accessible - that's what the What's wrong with >error message says ? > >More clumsy, but will work, is to compare with the name > > if (obj.GetType().ToString() = "System.RuntimeType") then If TypeOf obj Is System.Type Then Mattias -- Mattias Sjögren [C# MVP] mattias @ mvps.org http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com Please reply only to the newsgroup. 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. ;) > Trace.WriteLine(obj.GetType.ToString() & " " & (obj.GetType Is I suggested you use the TypeOf .. Is operator. 'Is' by itself does>GetType(Type)).ToString) 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. 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 Update to previous reply
TypeOf (obj) //VB worked where typeof (System.Type) // C# didn't. thanks, Alan. What did I say about $%^^ cross postings ;) "Fredrik Strandberg" <beachmount***@hotmail.com> wrote in message You'd be better off looking into Overloading and writing methods fornews:1141138374.183782.41340@u72g2000cwu.googlegroups.com... > 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). /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. Fredrik Strandberg wrote:
> Hi! The replies to this thread have astonished me.> > 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? 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 -- Larry Lard Replies to group please
View Source from WebBrowser (2005)
Menory compaction Please help with an 'impossible' error! navigation? datagrid and vertical gridline color Web service - business objects don't show up FTP with SSH Eventhandler FS: Visual Basic.NET VB.NET books for sale - CHEAP!!!!!! OnRenderMenuItemBackground question |
|||||||||||||||||||||||