C#接口方法歧义 [英] C# interface method ambiguity

查看:24
本文介绍了C#接口方法歧义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下示例:

接口 IBase1{整数百分比{得到;放;}}接口 IBase2{整数百分比{得到;放;}}接口 IAllYourBase : IBase1, IBase2{}类 AllYourBase : IAllYourBase{整数百分比;整数百分比{得到{回报百分比;}设置 { 百分比 = 值;}}}空 Foo(){IAllYourBase iayb = new AllYourBase();int 百分比 = iayb.Percentage;//编译失败.百分比"属性之间的歧义.}

在上面的示例中,要调用的 Percentage 属性之间存在歧义.假设 IBase1IBase2 接口不会改变,我将如何以最干净、最优选的方式解决这种歧义?>

更新

根据我使用显式接口实现得到的响应,我想提一下,虽然这确实解决了问题,但对我来说并没有以理想的方式解决它,因为我使用了我的 AllYourBase大多数情况下,对象作为 IAllYourBase,从不作为 IBase1IBase2.这主要是因为 IAllYourBase 也有由 AllYourBase 实现的接口方法(我没有在上面的代码片段中详细说明,因为我认为它们无关紧要)并且我想也可以访问这些.一直来回转换会变得非常乏味并导致代码混乱.

我确实尝试了一种解决方案,该解决方案涉及在 IAllYourBase 中定义 Percentage 属性而不使用显式接口实现,这似乎至少消除了编译器错误:

class IAllYourBase : IBase1, IBase2{整数百分比{得到;放;}}

这是一个有效的解决方案吗?

解决方案

明确实施:

公共类 AllYourBase : IBase1, IBase2{int IBase1.Percentage { get{ return 12;} }int IBase2.Percentage { get{ return 34;} }}

如果你这样做,你当然可以像平常一样对待你的非歧义属性.

IAllYourBase ab = new AllYourBase();ab.SomeValue = 1234;

但是,如果您想访问百分比属性,这将不起作用(假设确实如此,期望返回哪个值?)

int percent = ab.Percentage;//不管用.

您需要指定要返回的百分比.这是通过转换到正确的界面来完成的:

int b1Percent = ((IBase1)ab).Percentage;

如你所说,可以重新定义界面中的属性:

interface IAllYourBase : IBase1, IBase2{int B1Percentage{ 得到;}int B2Percentage{ 得到;}}类 AllYourBase : IAllYourBase{public int B1Percentage{ get{ return 12;} }public int B2Percentage{ get{ return 34;} }IBase1.Percentage { 得到 { 返回 B1Percentage;} }IBase2.Percentage { 得到 { 返回 B2Percentage;} }}

现在您已经通过不同的名称解决了歧义.

Consider the following example:

interface IBase1
{
   int Percentage { get; set; }
}

interface IBase2
{
   int Percentage { get; set; }
}

interface IAllYourBase : IBase1, IBase2
{
}

class AllYourBase : IAllYourBase
{
   int percentage;

   int Percentage {
      get { return percentage; }
      set { percentage = value; }
   }
}

void Foo()
{
   IAllYourBase iayb = new AllYourBase();
   int percentage = iayb.Percentage; // Fails to compile. Ambiguity between 'Percentage' property.
}

In the example above, there is ambiguity between which Percentage property to call. Assuming the IBase1 and IBase2 interfaces may not be changed, how would I go about resolving this ambiguity in the cleanest, most preferred way?

Update

Based on the responses I was getting for using explicit interface implementation, I want to mention that while this does solve the problem it does not solve it in an ideal way for me because I use my AllYourBase object as an IAllYourBase most of the time, never as an IBase1 or IBase2. This is mostly because IAllYourBase also has interface methods (I failed to detail those in my code snippet above because I thought they were irrelevant) that are implemented by AllYourBase and I want to access those too. Casting back and forth all the time will get very tedious and result in messy code.

I did try one solution that involved defining the Percentage property in IAllYourBase and not using explicit interface implementation, which seemed to get rid of the compiler error at least:

class IAllYourBase : IBase1, IBase2
{
   int Percentage { get; set; }
}

Is this a valid solution?

解决方案

Implement explicitly:

public class AllYourBase : IBase1, IBase2
{
    int IBase1.Percentage { get{ return 12; } }
    int IBase2.Percentage { get{ return 34; } }
}

If you do this, you can of course treat your non-ambigous properties just like normal.

IAllYourBase ab = new AllYourBase();
ab.SomeValue = 1234;

However, if you want to access the percentage prop this will not work (Suppose it did, which value would be expected in return?)

int percent = ab.Percentage; // Will not work.

you need to specify which percentage to return. And this is done by casting to the correct interface:

int b1Percent = ((IBase1)ab).Percentage;

As you say, you can redefine the properties in the interface:

interface IAllYourBase : IBase1, IBase2
{
    int B1Percentage{ get; }
    int B2Percentage{ get; }
}

class AllYourBase : IAllYourBase 
{
   public int B1Percentage{ get{ return 12; } }
   public int B2Percentage{ get{ return 34; } }
   IBase1.Percentage { get { return B1Percentage; } }
   IBase2.Percentage { get { return B2Percentage; } }
}

Now you have resolved the ambiguity by distinct names instead.

这篇关于C#接口方法歧义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆