473,503 Members | 9,735 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

type converter

Hi,

I need to convert between 2 identical structs,
but they are both in seperate libraries and so I cant just edit either
one of them to add explicit operators.

they are both 3d vectors, one is the managed directx Vector3,
containing float X,Y,Z, and the other is identical also containing float
X,Y,Z,
wich is also in a seperate library.

Idealy I need to be able to use any number of libraries wich
obtain the vector information I want wich each have their own definition of
Vector3
and to use any number of graphic linraries such as managed directx,
or Opengl binding etc, I also have my own graphics maths library wich uses a
vector3 wich I need to implement with both float and double vector3's.

I could have an interim struct wich has the converters,
but im wondering if theres a clever way posibly using TypeConverter ?

at the moment I have an overloaded static function wich does the conversion.
but it would be nice if I could pass the struct by reference and avoid any
copying where the structures are truly identical,
generics dont seem to be flexible enough to allow me to do what I need.

ie i cant access the X element inside a generic.

thanks
Colin =^.^=
Jun 27 '08 #1
4 1681
Hello Colin,

You could create one wrapper for both and then use the implicit operator to
do the casting.

See http://msdn.microsoft.com/en-us/libr...s2(VS.71).aspx

Regards,
Leo

"colin" <co*********@ntworld.NOSPAM.comwrote in message
news:Ic******************@newsfe17.ams2...
Hi,

I need to convert between 2 identical structs,
but they are both in seperate libraries and so I cant just edit either
one of them to add explicit operators.

they are both 3d vectors, one is the managed directx Vector3,
containing float X,Y,Z, and the other is identical also containing float
X,Y,Z,
wich is also in a seperate library.

Idealy I need to be able to use any number of libraries wich
obtain the vector information I want wich each have their own definition
of Vector3
and to use any number of graphic linraries such as managed directx,
or Opengl binding etc, I also have my own graphics maths library wich uses
a
vector3 wich I need to implement with both float and double vector3's.

I could have an interim struct wich has the converters,
but im wondering if theres a clever way posibly using TypeConverter ?

at the moment I have an overloaded static function wich does the
conversion.
but it would be nice if I could pass the struct by reference and avoid any
copying where the structures are truly identical,
generics dont seem to be flexible enough to allow me to do what I need.

ie i cant access the X element inside a generic.

thanks
Colin =^.^=
Jun 27 '08 #2
Well, it sounds like you are really after compile-time features;
TypeConverter might work, but it would be very slow (in comparison)
due to the extra layers - plus you wouldn't be able to add the
metadata to provide TypeDescriptor support.

Perhaps an extension method (C# 3) might be helpful... same thing as
the overloaded static function, but it appears (through compiler
trickery) as an instance method - i.e. someInstance.ToDXVector() via a
"public static {DXNamespace}.Vector3 ToDXVector(this
{SomeOtherNamespace}.Vector3 vector) {...}"

I would suggest some kind of dynamic .NET 3.5 "Expression" thingie,
but the generic type inference would be a pain - i.e. if you had an
extension method it would be "public static TTo
Convert<TFrom,TTo>(this TFrom value)", and the compiler can't make
this easily callable unfortunately (since it doesn't work with return-
type differences - of course, you could use an "out"...).

Marc
Jun 27 '08 #3
Actually - one other issue: you can't use "ref" with extension
methods, so that is out... sorry...

Overloaded static methods do seem your best option then... or just
live with the struct copying.

If you want, I have a similar example for Expression, but I don't
think it is the best option here...
Jun 27 '08 #4
Hi, thanks,
what happens when you call a function in a c++ library
with a parameter as a ref to a struct,
does it pass it as a pointer ?

can you make a c# function callable from a c++ library,
and could you then simply make it call the c# function
but as if it were going through the library interface,
and so just changing the parameter type ?

thanks
Colin =^.^=

"Marc Gravell" <ma**********@gmail.comwrote in message
news:c4**********************************@z72g2000 hsb.googlegroups.com...
Actually - one other issue: you can't use "ref" with extension
methods, so that is out... sorry...

Overloaded static methods do seem your best option then... or just
live with the struct copying.

If you want, I have a similar example for Expression, but I don't
think it is the best option here...

Jun 27 '08 #5

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

Similar topics

5
2904
by: jorfei | last post by:
I have written a component with a property IPAdrress of type System.Net.IPAddress. To ease the configuration of the component at design time, I have written a type converter for the type...
6
5634
by: Kerry Sanders | last post by:
I am working on a project for work where I need a specialized type converter to convert the value of a string which is edited in a grid back to the underlying object type from the cell. The value...
2
1575
by: Claire | last post by:
I want to pass the type of an object as a field in an object. public class FileEventArgs : EventArgs { public object Data = null; public Type ClassType = null;
5
2290
by: Pavils Jurjans | last post by:
Hello, I am somewhat lost in the implicit/expicit possible/impossible type casting in C#... I need to write a class, which among other things, must have wat to read a numeric value type, and...
3
1462
by: Shane | last post by:
I thought that arrays and hash tables could be strictly typed under 2.0 instead of just being objects, but I can't find any documentation. I want to have the values in a hash table designated as...
8
2363
by: bonk | last post by:
When I have an instance of an object wich is of type System.Type, how can I find out if it directly or indirecly derives from another type? myTypeInstance == typeof(AnotherType) only seems to...
10
2455
by: esha | last post by:
I tried several online converters. In many case they do the job, but sometimes give some mess. I think that all converters I know are old, were created for VS 2003 and do not understand new stuff...
3
5786
by: AAJ | last post by:
Hi has anyone come across a function to check if a particular string can be safely converted to a datatype i.e. i would like to check things like TypeCheck("1/1/2006",datetime) -returns...
2
3645
by: =?Utf-8?B?TWFyZWs=?= | last post by:
Hi I have a class which has its own type converter that adds some additional properties using overridden GetPropertiesSupported and GetProperties methods. All works fine if an instance of the...
11
10084
by: =?Utf-8?B?bWljaGFlbCBzb3JlbnM=?= | last post by:
I have worked with application settings in VS2005 and C# for awhile, but usually with standard types. I have been trying to store a custom container/class/type in an application setting and I have...
0
7207
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
7015
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
7470
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...
0
5602
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,...
1
5026
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...
0
3183
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...
0
3173
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1523
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 ...
0
403
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...

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.