实现相同的通用接口2次不同的泛型参数接口、不同、参数

2023-09-06 14:09:55 作者:爷霸凌全服

我必须实现2接口,同时具备如下不同的泛型参数。我感到困惑还不够了解。我不知道哪一个迭代本身的foreach。现在我明白了第一个被隐含choosen。

I had to implement 2 interface same time with different generic parameter as below. I get confused enough about it. I had no idea which one of them iterate itself in foreach. Now i understand first one is implicitly choosen.

我已经试过新BarList()的GetEnumerator()但我不能在指定方法级的类型参数。

I have tried new BarList().GetEnumerator() but i can not specify type parameter on method level.

唯一能做的我发现它的铸造它的接口一样(新BarList()为IEnumerable<字符串>

Only solution i have found it that casting it to interface like(new BarList() as IEnumerable<string>)

混乱它足够之后。我只是想知道,这样的设计是不是真的好主意吗?我必须避免执行相同的通用接口,一个更多的时间?

After confusing about it enough. I just wanted to know that this design is not really good idea ? I have to avoid to implement same generic interface one more time ?

class Program
{
    static void Main(string[] args)
    {
        foreach (var item in new BarList())
        {

        }
    }
}

class BarList: IEnumerable<string>, IEnumerable<int>
{

    public IEnumerator<int> GetEnumerator()
    {
        throw new NotImplementedException();
    }

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        throw new NotImplementedException();
    }

    IEnumerator<string> IEnumerable<string>.GetEnumerator()
    {
        throw new NotImplementedException();
    }
}

修改

让我解释一下为什么我会这样。

Let me explain why i am going in this way.

我不得不实施 IPagedList&LT; T&GT; 这是从继承接口的IList&LT; T&GT; 。我想写的扩展方法,将其转换为我的视图模型。像下面

I had to Implement IPagedList<T> interface which is inherited from IList<T>. I wanted to write extension method which convert it to My view model. like below

GETALL()ToPagedList(PageIndex的); //这是返回IPagedList然后我想使用它像以下;

GetAll().ToPagedList(pageindex);//which is returning IPagedList Then i wanted to use it like below;

GetAll().ToPagedList(pageindex).ToViewModel<T,TViewModel>();

有关实现这一点我试图返回 IPagedList&LT;视图模型&GT; 该扩展method.In这种情况下,我要实现IPagedList 2次使用不同的参数。但是,这种策略取得了令人困惑的事情。这就是原因吧。

For achieve this I tried to return IPagedList<ViewModel> by that extension method.In that case I have to implement IPagedList 2 times with different parameter. But this strategy made confusing things. This is reason of it.

推荐答案

编译器正在挑选的IEnumerator&LT; INT&GT;的GetEnumerator 按照规则在C#语言规范,它的的8.8.4方法首先的寻找一个可访问的GetEnumerator() BarList 输入法。那些可用的只有一个,就是一个返回的IEnumerator&LT; INT&GT;

The compiler is picking the IEnumerator<int> GetEnumerator method by following the rules in 8.8.4 of the C# language specification which first looks for an accessible GetEnumerator() method on the BarList type. The only one of those which is available is the one returning IEnumerator<int>.

如果你做了的是的方法使用显式接口实现,以及,那么它会去到部分8.8.4,其中规定,如果有多于一个类型T这样的后期阶段有从ex pression型( BarList 这里)的隐式转换为的IEnumerable&LT; T&GT; 然后产生一个错误。

If you had made that method use explicit interface implementation as well, then it would have gone onto the later stages of section 8.8.4, which states that if there is more than one type T such that there is an implicit conversion from the expression type (BarList here) to IEnumerable<T> then an error is produced.

我会说这的是的一个令人困惑的设计 - 我可能会增加属性或方法来检索的数据适当的意见

I would say this is a confusing design - I would probably add properties or methods to retrieve appropriate "views" on the data.