Mirtul,
As a general rule, reflection and generics are not going to go together.
That is, with reflection, you are going to have a run-time check against the
type, while generics are going to be evaluated by the compiler at compile
time.
You should be just casting your interface, something like this:
IMyInterface i = ...
MyClass instance = (MyClass) instance;
Now, something tells me that you are using reflection because you don't
have a reference to the actual type that you want to cast to.
Unfortunately, you can't perform casts like that. If you don't know the
type at compile-time, then there is no way for the compiler to verify any
claims you make against that type (for instance, accessing fields,
properties, methods, etc, etc).
So you either have to have a reference to the type directly at compile
time, or you have to have the type derive from another base class/interface
which will expose the functionality you need.
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
-
mv*@spam.guard.caspershouse.com
"Mirtul" <Mi****@discussions.microsoft.comwrote in message
news:06**********************************@microsof t.com...
Hi
I'm currently working with vbscripting through MSScriptControl. We have
shared some of our objects that should be available for scripting.
Some of the functions of these objects will return an interface to a class
that must be cast to the appropriate higher level class to use all
functionality.
Thus I tried to implement a generic cast function for use from the
vbscript
side that would take an object and a typestring and convert the object to
chosen type.
Ex: set myObject = Converter.Convert(myInterfaceObject,
"MyProgram.MyNamespace.MyObject")
To be able to use the object fully, I would have to return an object of
the
correct type. I tried to implement this via generics and instancing via
reflection, but I always seemed to fail on the point where I could not
have a
function return a generic without somewhere specifying hardcoded what that
generic would be.
Has anyone been able to implement a cast function usable in this manner?
Or
is there a support in the framework for doing it?
Thanks for your help.
/K