“Interface not implemented” when Returning Derived Type

前端 未结 12 825
野趣味
野趣味 2020-11-27 21:11

The following code:

public interface ISomeData
{
    IEnumerable Data { get; }
}

public class MyData : ISomeData
{
    private List

        
12条回答
  •  一整个雨季
    2020-11-27 22:04

    Hmm, is it a shortcoming, I would say no.

    In either way, I would work around it like darin's answer, or, if you explicitly want a List accessor as well, you could do it like this:

    public class MyData : ISomeData
    {
    
        IEnumerable ISomeData.Data
        {
            get
            {
                  return _myData;
            }
        }
    
        public List Data
        {
              get
              {
                 return (List)((ISomeData)this).Data;
              }
        }
    
    }
    

提交回复
热议问题