473,769 Members | 4,173 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Conve rt(myInterfaceO bject,
"MyProgram.MyNa mespace.MyObjec t")

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 6001
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.co m

"Mirtul" <Mi****@discuss ions.microsoft. comwrote in message
news:06******** *************** ***********@mic rosoft.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.Conve rt(myInterfaceO bject,
"MyProgram.MyNa mespace.MyObjec t")

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.co m

"Mirtul" <Mi****@discuss ions.microsoft. comwrote in message
news:06******** *************** ***********@mic rosoft.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.Conve rt(myInterfaceO bject,
"MyProgram.MyNa mespace.MyObjec t")

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****@discuss ions.microsoft. comwrote in message
news:06******** *************** ***********@mic rosoft.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.Conve rt(myInterfaceO bject,
"MyProgram.MyNa mespace.MyObjec t")

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.Conve rt<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
1722
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 implement the interface IUser. But this did not work... I thought that as User implement IUser it was possible to cast the generic
3
2772
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 dead ends. I'm posting this to get some feedback on wether I'm going in the right direction, and at the same time hopefully save others from going through the process.
4
3243
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 subclass of Logical (LogicalA, LogicalB). I'm currently casting them to the right type when I retrieve them, but obviously this isn't terribly 'nice'. I tried converting the class to be generic, and having a type parameter that affected the type of...
3
4997
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 the enum, this wouldn't compile so I tried a few variations. The cast fails inside the generic function even though its valid. The code below is the simplest example of the problem I could think of, even casting int to int fails.
2
3519
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 manipulate the bitfields. For instance: <Flags()_ Enum ActionFlags As Byte
7
2040
by: Dave | last post by:
I've got these declarations: public delegate void FormDisplayResultsDelegate<Type>(Type displayResultsValue); public FormDisplayResultsDelegate<stringdisplayMsgDelegate; instantiation: displayMsgDelegate = DisplayStatusMessage; implementation: public void DisplayStatusMessage(string statusMessage)
4
7844
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 code below, ColumnMap is a simple struct which basically has three properties, Header (a string), Index (an int), TypeOfData (which is a DataType which is a local eNum). _Mapping is a local List<ColumnMap>. public ColumnMap GetMap<T>(T value)
8
3154
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 formatting) to show what my issue is. I have a generic abstract class here: public abstract class MyGenericAbstractClass<Twhere T : MyInterface
2
4183
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 types derived from that base type, or arrays or generic collections of instances of types derived from that base type. All is well until I come to the properties which are generic collections, I don't seem to be able to find an elegant way of...
0
10222
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10050
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9866
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8876
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5310
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3967
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.