Well, you can pass an array of parameters to CreateInstance(), but you need to have some idea of the object type, otherwise you'll never be able to use it as anything other than an Object (how would you know which methods to call?). If you're expecting, say, an ICollection, you could attempt to cast to ICollection, and throw an exception if the cast fails:
Dim foo as ICollection = DirectCast(obj, ICollection) (My VB is a little shaky, but I think that's right). Jim > -----Original Message----- > From: Ben Hodgson [mailto:[EMAIL PROTECTED]] > Sent: 02 May 2002 11:39 > To: [EMAIL PROTECTED] > Subject: [DOTNET] Reflection - casting Object at runtime > > > I'm passing an object into a method, which accepts it as an > Object. The > method then needs to use this object in its original type, > which it does a > bit like this: > > Public Function SomeFunction(ByVal obj as Object) As Boolean > Dim newObj as Object > newObj = Activator.CreateInstance(obj.GetType()) > newObj = obj > 'Use newObj to do the work > 'etc... > > However, I may be passing in objects with no public or parameterless > constructors, which seems to be a problem for > "Activator.CreateInstance". > Given I have my object (albeit as an Object) and its type, > which doesn't > have a public constructor, how do I create a new object > ("newObj") of the > correct type? Alternatively, how do I cast my object ("obj") > to its type, > which right now seems a more likely solution? > Thanks in advance for any help (doesn't have to be VB). > > You can read messages from the DOTNET archive, unsubscribe > from DOTNET, or > subscribe to other DevelopMentor lists at http://discuss.develop.com. > You can read messages from the DOTNET archive, unsubscribe from DOTNET, or subscribe to other DevelopMentor lists at http://discuss.develop.com.