我自己的托管类型作为c++ /CLI类库中的参数:CS0570:不受语言支持



我一直在尝试开发一个c++/CLI库用于c#,我有以下问题。如果我们把我的托管引用类如下所示:

namespace Library
{
using namespace System;
public ref class Test
{
internal:
    String^ internalString;
public:
    Test()
    {
        internalString = gcnew String("Hey There");
    }
    ~Test()
    {
    }
};
public ref class TestImplement
{
public:
    static String^ TestMessage(Test test)
    {
        return test.internalString;
    }
};
}

和我的c#实现如下:

使用系统;

namespace AddProgram
{
class Program
{
    static void Main(string[] args)
    {
        Library.Test test = new Library.Test();
        Console.WriteLine(Library.TestImplement.TestMessage(test));
        Console.Read();
    }
}
}

我得到以下错误:

错误CS0570:语言不支持'TestMessage'

据我所知,这是由于传递类型库。Test作为参数。我不明白为什么我得到这个消息,我希望有可能从我的参考库传递类型。

如有任何帮助,不胜感激

您需要将TestMessage声明为接受对库的引用。Test,这意味着使用插入符号(^),就像您对String^所做的那样。c++/CLI允许你通过去掉插入符号来使用值类型语义(某种程度上)处理引用类型,但是c#没有相应的功能,这就是为什么你会得到这个错误。

最新更新