Is it possible to find the System.Type of the expected function return type in VB.NET?

T

Tony Cox

Hi. I'm new to VB.NET, so please be gentle ;-)

I'm trying to transfer VB.NET structures to a shared memory area
at a fixed location in process memory space. The structures themselves
consist of simple data types (integers, shorts, etc.), and the data transfer
to/from the shared memory area is handled with the Marshalling routines
from System.Runtime.InteropServices. All seems to work ok.

I've implemented a class "MMVb" to handle the details of the data transfer,
and to simplify the end-user interface, but the syntax is not as clean as
I'd have
hoped. The idea is to be able to handle any type of UDT, all though the same
class property, like this:-

Dim mapping As MMVb = new MMVb(...) ' Object to handle xfer
Dim f As fred '
User-defined struct
....
mapping.struct = f '
Transfer to shared memory

So far so good. The last statement calls the 'struct' property set routine,
defined as "Set(ByVal Value As Object)". I can happily call
Marshal.StructureToPtr(..) to transfer the data and all works fine.

Now what I'd like to do is reverse the procedure to reconstruct the data,
like this:-

f = mapping.struct
'Transfer back to VB.NET

but of course the method Marshal.PtrToStructure requires the
System.Type of the structure it is reconstructing. For arbitrary
objects, this isn't known. I've tried adding an optional parameter
to the 'struct' property definition, and of course this works but
leads to messy syntax like this:-

f = mapping.struct( f)
'Parameter needed to work out return type

and I suppose I could always just define things as methods
passing 'f' by reference leading to syntax like this:-

mapping.getStruct( f)

So, is it possible to find the type of the expected returning data,
so that (in this case) I can determine it from the Property Get
routine?

Thanks for any insight....
 
M

Mattias Sjögren

Tony,
So, is it possible to find the type of the expected returning data,
so that (in this case) I can determine it from the Property Get
routine?

No, in this case all you could find out was that the method returns
Object, not what that object is assigned to.

I'd add a System.Type parameter indicating the requested return type,
and call it with

f = mapping.GetStruct(GetType(fred))



Mattias
 
T

Tony Cox

Mattias Sjögren said:
Tony,


No, in this case all you could find out was that the method returns
Object, not what that object is assigned to.

Thanks. Saved me a lot of further messing around.
 

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