使用重写属性对重载函数进行模糊调用

时间:2015-08-03 00:19:44

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

我有两个基本接口

interface IHasId
{
    int Id {get;} // readonly 
}

interface IEntity : IHasId
{
    new int Id {get; set;} // must be able to assign Ids
}

然后有几个实体实现了IEntity。

interface IThing : IEntity
{
   // thing specific stuff
}

这些实体在C#和C ++ / Cli中都被使用。这一切都很好,直到我搬到VS2015。

现在,当我尝试在C ++中访问Id时

void SomeFunction(IThing^ thing)
{ 
    DoSomething(thing->Id);
}

我收到以下错误:

error C2668: 'IHasId::Id::get': ambiguous call to overloaded function
1>  file.cpp(52): note: could be 'int IHasId::Id::get(void)'
1>  file.cpp(52): note: or       'int IEntity::Id::get(void)'

我可以通过将代码更改为

来解决此问题
void SomeFunction(IThing^ thing)
{ 
    IHasId x = thing;
    DoSomething(x->Id);
}

但转换所有现有代码是一项艰巨的任务。这在2010年运作良好。有人有任何建议吗?

1 个答案:

答案 0 :(得分:0)

您可以将此代码复制到一个非常简单的项目中并确认它仍然无效吗?在你的例子中,你没有触及IEntity,这让我觉得你的项目中还有其他代码以某种方式将这些接口联系在一起。你所展示的内容似乎不太可能。

相关问题