c# - C++ CLI 句柄类型

标签 c# c++ .net c++-cli

在以下代码中获取non-handle类型的正确方法是什么:

template <typename Type> ref class SuperClass
{
public:
    void Method()
    {
        Type x = gcnew ???? (...);
        // I want it to be instantiated into 'String^ x = gcnew String(...).
        // Is there a way to "dereference" the handle type in C++ \ CLI ?
    }
};

SuperClass<String^> superClass;
superClass.Method(); // <---- Won't compile

此外,使用句柄类型作为模板参数是强制性的(这是更大示例的一部分,我不能简单地将模板类型更改为String 而不是 String^)。

最佳答案

gcnew 始终返回句柄 (^)。
所以你可以尝试一下。不确定它是否真的满足您的需求 -

<罢工> 模板引用类父类(super class) { 民众: 无效方法() { Type^ x = gcnew Type("Hello"); } };

<罢工>
SuperClass<String> superClass;
superClass.Method();

<罢工>

template <typename Type> ref class SuperClass
{
public:
    void Method()
    {
    Type x = "Hello";
    }
};

SuperClass<String^> superClass;
superClass.Method();

关于c# - C++ CLI 句柄类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10942559/

相关文章:

c++ - 使用 std::istream::operator>> 处理无符号类型,我如何区分下溢提取失败?

c# - 如何只查找同时具有 getter 和 setter 的属性?

c# - out 用于多个输出值或返回组合值类型更好吗?

c# - Linq to Sql 中的数据类型转换

C# 如何捕获异常并检查它是否包含字符串?

c++ - 带有 std::variant 或 union 包装器的通用接口(interface)

c# - Azure 表存储 - 索引?

c# - 从私有(private)void函数c#中的另一个类访问变量

c# - 如何检查是否未分配 DateTime 对象?

c++ - LibAV FFmpeg C++ av_find_input_format ("dshow") 返回 nullptr