Generic casting

G

Guest

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
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
G

Guest

Hi Nicholas

Thanks for the answer, time for me to stop searching for the impossible then
:).

I'll just create a specific function for each cast then. I was hoping to
avoid it somehow, but at least now I know.

/K


Nicholas Paldino said:
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]
- (e-mail address removed)

Mirtul said:
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
 
B

Ben Voigt

Mirtul said:
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?

I guess you are working around lack of support for a simple cast syntax in
VBScript? You should be able to get this syntax to work:
 

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