C#接口方法模糊 [英] C# interface method ambiguity

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

问题描述

请看下面的例子:

 接口IBase1 
{
INT百分比{搞定;组; }
}

接口IBase2
{
INT百分比{搞定;组; }
}

接口IAllYourBase:IBase1,IBase2
{
}

类AllYourBase:IAllYourBase
{
INT百分比;

INT百分比{
{返回百分比; }
集合{百分比=价值; }
}
}

无效美孚()
{
IAllYourBase iayb =新AllYourBase();
INT百分比= iayb.Percentage; //编译失败。 百分比属性之间的模糊。
}

在上面的例子中,有歧义其间百分比物业打电话。假设 IBase1 IBase2 接口可能的的改变,我将如何去解决这种模糊性在最清洁,最首选的方法是什么?



更新



根据我正在使用的反应显式接口实现,我想提一提,虽然这并不能解决问题不解决它以理想的方式适合我,因为我用我的 AllYourBase 对象作为 IAllYourBase 的大部分时间,从来没有作为一个 IBase1 IBase2 。这主要是因为 IAllYourBase 也有接口的方法(我没有详细说明这些在我的代码片段上面,因为我以为他们是无关)由实施AllYourBase 我要访问这些呢。铸造来回所有的时间会很繁琐,导致乱码。



我也尝试一个解决方案,涉及定义百分比 IAllYourBase ,而不是使用显式接口实现,这似乎至少要摆脱编译器错误的:

 类IAllYourBase:IBase1,IBase2 
{
INT百分比{搞定;组; }
}

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


< DIV CLASS =h2_lin>解决方案

实施明确的:

 公共类AllYourBase:IBase1,IBase2 
{
INT IBase1.Percentage {{返回12; }}
INT IBase2.Percentage {{返回34; }}
}

如果你这样做,你当然可以把你的非ambigous 。物业就像正常的

  IAllYourBase AB =新AllYourBase(); 
ab.SomeValue = 1234;



不过,如果您要访问的百分比支撑,这将不起作用(假设它没有,它的价值?将回报预期)

  INT百分比= ab.Percentage; // 不管用。 

您需要指定的该百分比的返回。这是通过强制转换为正确的界面来完成:

  INT b1Percent =((IBase1)AB)百分比; 



就像你说的,你可以重新定义接口的属性:

 接口IAllYourBase:IBase1,IBase2 
{
INT B1Percentage {搞定; }
INT B2Percentage {搞定; }
}

类AllYourBase:IAllYourBase
{
公众诠释B1Percentage {{返回12; }}
公众诠释B2Percentage {{返回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天全站免登陆