【问题标题】:My own managed type as arg in C++/CLI class library : CS0570: is not supported by the language我自己的托管类型作为 C++/CLI 类库中的 arg:CS0570:语言不支持
【发布时间】:2011-12-29 05:50:26
【问题描述】:

我一直在尝试开发用于 C# 的 C++/CLI 库,但遇到以下问题。如果我们将我的托管引用类如下:

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”

据我所知,这是由于将类型 Library.Test 作为参数传递。我不明白为什么会收到此消息,我希望可以从我的参考库中传递类型。

任何帮助将不胜感激

【问题讨论】:

  • 在 C++/CLI 中注意你的帽子和析构函数。

标签: .net c#-4.0 interop c++-cli


【解决方案1】:

您需要将TestMessage 声明为对Library.Test 进行引用,这意味着使用插入符号(^),就像对String^ 所做的那样。 C++/CLI 允许您使用值类型语义(在某种程度上)通过省略插入符号来处理引用类型,但 C# 没有等效功能,这就是您收到该错误的原因。

【讨论】:

  • 我遇到了同样的问题,因为我的方法签名是 MyMethod(System::Collections::Generic::List<System::String^> myArgs),即我忘记将 List 作为引用以及通用 arg。
猜你喜欢
  • 2016-09-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-07
  • 1970-01-01
  • 2013-09-12
  • 2017-10-15
相关资源
最近更新 更多