加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 实现冲突的接口

发布时间:2020-12-16 01:53:42 所属栏目:百科 来源:网络整理
导读:如果两个接口具有不同类型的相同属性,我如何使用包含它们的类.示例代码: internal interface ITest{ string WhatEv { get; set; }}internal interface ITest2{ int WhatEv { get; set; }}internal interface ITest3 : ITest,ITest2{}internal class Simple
如果两个接口具有不同类型的相同属性,我如何使用包含它们的类.示例代码:

internal interface ITest
{
    string WhatEv { get; set; }
}

internal interface ITest2
{
    int WhatEv { get; set; }
}

internal interface ITest3 : ITest,ITest2
{
}

internal class Simple : ITest3
{
    string ITest.WhatEv { get; set; }

    int ITest2.WhatEv { get; set; }
}

internal class Program
{
    public static void Main(string[] args)
    {
        ITest3 foo = new Simple();
        foo.WhatEv = "asdf";
    }
}

Ambiguous引用错误的屏幕截图:

解决方法

您将foo转换为ITest或ITest2.

ITest3 foo = new Simple();
((ITest)foo).WhatEv = "asdf";

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读