System:Uint32^ in C++/CLI compiles to VaueType in C#?

旧巷老猫 提交于 2021-02-11 04:26:15

问题


In a C++/CLI class library I have the following function:

static System::UInt32^ Fn()
{
    return gcnew System::UInt32(0);
}

When I use the class library in a C# the System::Uint32 is a System::ValueType and the compiler error:

Error 1 Cannot implicitly convert type 'System.ValueType' to 'uint'. An explicit conversion exists (are you missing a cast?)

Why is it that when I'm defiing a System::UInt32 in C++/CLI I get a ValueType in C#?

Thanks


回答1:


UInt32 is a value type (struct in C#), not a reference type. Therefore, it does not need the ^ handle.

Switch that method to the following, and it'll appear normally in C#.

static System::UInt32 Fn()
{
    return 0;
}

If you convert the rest of your code to remove the ^ from the value types, you'll find that your C++/CLI code looks more natural, and you'll have less issues interfacing with the .Net Library.



来源:https://stackoverflow.com/questions/25726248/systemuint32-in-c-cli-compiles-to-vauetype-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!