473,406 Members | 2,217 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Generic casting

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

May 15 '07 #1
3 5988
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
May 15 '07 #2
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 [.NET/C# MVP]" wrote:
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
May 15 '07 #3

"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?
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:

Converter.Convert<DesiredType>(expression)
>
Thanks for your help.

/K

May 15 '07 #4

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

Similar topics

2
by: cylt | last post by:
Hi, I would like to have something like that : IList<IUser> list2 = list1 as IList<IUser>; where list1 is a generic collection of User ( IList list1<User>=new List<User>() ) and where User...
3
by: Tigger | last post by:
I have an object which could be compared to a DataTable/List which I am trying to genericify. I've spent about a day so far in refactoring and in the process gone through some hoops and hit some...
4
by: Andrew Ducker | last post by:
I have a collection of classes descending from a single root class (let's call it RootClass). They all currently have a property of Logical, of type Logical. However they actually return a...
3
by: Vincent Finn | last post by:
Hi, I am trying to write a generic function and it isn't behaving as expected. I want to avoid having to write custom convert functions for my enums so I want to convert to an int and cast to...
2
by: JB | last post by:
Hi All, I'm pulling my hair over this and could really do with a bit of help. I'm using various different enums as bit fields and I'd like to create a set of generic Functions or Subs to...
7
by: Dave | last post by:
I've got these declarations: public delegate void FormDisplayResultsDelegate<Type>(Type displayResultsValue); public FormDisplayResultsDelegate<stringdisplayMsgDelegate; instantiation:...
4
by: =?Utf-8?B?RXRoYW4gU3RyYXVzcw==?= | last post by:
Hi, I have written a generic method which does different things depending on the type of the parameter. I got it to work, but it seems really inelegant. Is there a better way to do this? In the...
8
by: MMAS | last post by:
Hey everyone -- Curious about some strange behaviour I'm seeing that seems to be related to my lack of understanding on how generics work in C#. Here's some simplified code (sorry for strange...
2
by: SimonDotException | last post by:
I am trying to use reflection in a property of a base type to inspect the properties of an instance of a type which is derived from that base type, when the properties can themselves be instances of...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.