By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,638 Members | 2,249 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,638 IT Pros & Developers. It's quick & easy.

CLI ref structs. Is there any benefit to using it.

P: n/a
Hi,

I'm coding a layer into my application using CLI, between a unmanaged
and a c# layer. So, I have to marshal some unmanaged c++ structures
to structures usable in c#.

My solution was to marshal the unmanaged C++ structures to CLI ref
structs, these being usable in the c# layer. Why I did it this way is
I want all of the business logic in the CLI layer. Because he c#
layer is purely presentaion.

My question is, what is the point of using CLI ref structs?

Apr 19 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
I'm coding a layer into my application using CLI, between a unmanaged
and a c# layer. So, I have to marshal some unmanaged c++ structures
to structures usable in c#.

My solution was to marshal the unmanaged C++ structures to CLI ref
structs, these being usable in the c# layer. Why I did it this way is
I want all of the business logic in the CLI layer. Because he c#
layer is purely presentaion.

My question is, what is the point of using CLI ref structs?
Hi,

the point of using ref structs is that they translate to classes with public
members on the C# side without the need for marshalling. As a result, you
can pass those structures to the C# side or to .NET methods.
If these structures do not leave the CLI side, there is no point to
marshaling them because C++/CLI is designed to use unmanaged structures and
classes without a problem. In that case, marshaling them to managed
stuctures is redundant.

--
Kind regards,
Bruno van Dooren MVP - VC++
http://msmvps.com/blogs/vanDooren
br**********************@hotmail.com
Apr 20 '07 #2

P: n/a
On Apr 20, 1:50 am, "Bruno van Dooren"
<bruno_nos_pam_van_doo...@hotmail.comwrote:
I'm coding a layer into my application using CLI, between a unmanaged
and a c# layer. So, I have to marshal some unmanaged c++ structures
to structures usable in c#.
My solution was to marshal the unmanaged C++ structures to CLI ref
structs, these being usable in the c# layer. Why I did it this way is
I want all of the business logic in the CLI layer. Because he c#
layer is purely presentaion.
My question is, what is the point of using CLI ref structs?

Hi,

the point of using ref structs is that they translate to classes with public
members on the C# side without the need for marshalling. As a result, you
can pass those structures to the C# side or to .NET methods.
If these structures do not leave the CLI side, there is no point to
marshaling them because C++/CLI is designed to use unmanaged structures and
classes without a problem. In that case, marshaling them to managed
stuctures is redundant.

--
Kind regards,
Bruno van Dooren MVP - VC++
http://msmvps.com/blogs/vanDooren
bruno_nos_pam_van_doo...@hotmail.com
I'm not sure I understand. Why wouldn't I just use a CLI class then.
What's the difference? Is there any real benefit though?

Apr 20 '07 #3

P: n/a
I'm not sure I understand. Why wouldn't I just use a CLI class then.
What's the difference? Is there any real benefit though?
Are you asking about "ref struct vs ref class" or "ref struct vs native
class" or "ref struct vs value struct"?

ref struct and ref class are related in the same way as native struct and
native class -- there is no runtime difference. The only thing affected is
that struct, ref struct, and value struct all start with implicit "public:"
and class, ref class, and value class all start with implicit "private:".
ref class and ref struct are both C++/CLI equivalents to C# struct, all
three generate a garbage collected pass-by-ref CLR type, and all three have
different default member visibility.
Apr 20 '07 #4

P: n/a
DaTurk wrote:
I'm not sure I understand. Why wouldn't I just use a CLI class then.
What's the difference? Is there any real benefit though?
C++/CLI -------C#
====================
ref class -----class
ref struct ----class
value class ---struct
value struct --struct

The C++/CLI keyword "ref" always indicates a reference type (C# class),
while the "value" keyword always refers to a value type (C# struct).

What confuses you is that in C# class and struct mean completely
different things than in C++. In C++, the only difference between class
and struct is whether members are public or private by default. In C#,
on the other hand, a class is a reference type, and a struct is a value
type, and they have nothing to do with public vs private access.

Tom
Apr 20 '07 #5

P: n/a
Ben Voigt wrote:
ref class and ref struct are both C++/CLI equivalents to C# struct
I think you meant C# class.

Tom
Apr 20 '07 #6

P: n/a

"Tamas Demjen" <td*****@yahoo.comwrote in message
news:OT****************@TK2MSFTNGP05.phx.gbl...
Ben Voigt wrote:
>ref class and ref struct are both C++/CLI equivalents to C# struct

I think you meant C# class.
Yes indeed.

ref class and ref struct are both C++/CLI equivalents to C# class
value class and value struct are both C++/CLI equivalents to C# struct

Thanks for calling out my typo.
Apr 20 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.