type converter

C

colin

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 =^.^=
 
M

Marc Gravell

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
 
M

Marc Gravell

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

colin

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 =^.^=
 

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