开发者

c# equivalent for c++/cli long

开发者 https://www.devze.com 2023-03-25 12:22 出处:网络
I declared a method of in C# like this: [OperationContract] [FaultContract(typeof(MyException))] MyClass MyMethod(... some params ..., Int32[] myParam);

I declared a method of in C# like this:

[OperationContract]
[FaultContract(typeof(MyException))]
MyClass MyMethod(... some params ..., Int32[] myParam);

And in C++/CLI a need to write the method matching the interface:

开发者_如何学C
MyClass^ MyMethod(... some params ..., array<long>^ myParam) { ...

I need to trasfer array of longs for C++ world from .Net. I know that C++ long is not the .Net long. But I don't know how to make this.


What is wrong with System::Int32 (or simply Int32 since most C++/CLI source files have using namespace System) ?

MyClass^ MyMethod(... some params ..., array<Int32>^ myParam) 

As a rule, use .NET types when you talk to .NET.


In the C++ compiler for MSVC, long and int have the same size. I'm not sure if you're thinking of long long which represents a 64-bit signed integer in MSVC. If you mean just long though, then Int32 within .Net should be fine.

To be really safe, you can use the provided macros for signed 32-bit integers:

#include <cstdint>

MyClass^ MyMethod(... params ..., array<int32_t>^ myParam) { ...

Or better yet, using the .NET defined types as Alexandre C recommends:

MyClass^ MyMethod(... params ..., array<System::Int32>^ myParam) { ...

The most important thing to take note of is that int, long, int32_t, and System::Int32 are all the same exact size in the current MSVC C++ compiler. It doesn't matter which you use, but int32_t and System::Int32 are the safest choices.

Microsoft can change their long at a later date to be a size larger 32-bits. If that were to happen, then you could recompile this same code with the new compiler with zero issues.

With regards to what size each data type is, the standard requires that int and long are at least 4 bytes large. On some compilers, you may find that sizeof(long) != sizeof(int). For that reason, if you want to make sure that you're using integers that are exactly 4 bytes big you should use the provided headers that guarantee the required size.

For more details see here: http://en.wikipedia.org/wiki/Long_integer. The article includes the relevant links to the standards.


long and int in C++ have same size (4 bytes) and are both mapped to .NET type System.Int32. However, long, when used as a parameter or in a field, has the optional modifier System.Runtime.CompilerServices.IsLong (in CIL, it is int32 modopt([mscorlib]System.Runtime.CompilerServices.IsLong))

This allows you to create method overloads differing only in int/long parameter.

0

精彩评论

暂无评论...
验证码 换一张
取 消