C++/cli 接口的属性不能从 c# 中使用

C++/cli interface with properties not usable from c#

Cli 界面如下所示:

using namespace System::Timer
namespace Interfaces
{
    public interface class ITimerProvider
    {
        property Timer AppTimer
        {
             Timer get();
        }
    }
}

获取从该接口派生的 c# class 并使用 VS2013 中右键单击菜单中的 "implement interface",它创建:

public void get_AppTimer(ref Timer value)
{
   throw new NotImplementedException();
}

编译器报错"MyProject does not implement interface member MyCLIProject.Interfaces.ITimerprovider.get_AppTimer()"

它会这样做,即使它自己把它放进去也是如此。

我认为这可能是由于 Visual Studio 没有为您正确生成代码。

虽然从技术上讲,属性只是方法 get_propertyName()set_PropertyName() 的语法糖,但在实现 属性 时,您实际上并没有在 C# 中编写这些方法。实现 属性 的正确 C# 代码是这样的:

class MyProject
{
    public Timer AppTimer
    {
        get
        {
            // return the value here
        }
    }
}

如果您将代码更改为类似这样的内容,应该可以修复错误。

汉斯给出了答案。更正接口声明会导致预期的自动生成代码并且项目编译正常:

property Timer^ AppTimer
    {
         Timer^ get();
    }