Problem with interface implementation in partial classes

不羁岁月 提交于 2019-11-28 12:12:25
JJoos

You can solve this by implementing the interface explicitly:

namespace PartialProject.objects
{
  public interface Interface
  {
    Interface Instance { get; }
  }

  //To make sure the autogenerated code inherits Interface
  public partial class Class : Interface 
  {
    Interface Interface.Instance 
    {
      get
      {
        return Instance;
      }
    }
  }

  //This is autogenerated
  public partial class Class
  {
     public Class Instance
     {
        get
        {
          return this.Instance;
        }
     }
  }
}

Return types aren't covariant in C#. As you can't change the auto-generated code the only solution I see is to change the interface:

public interface Interface<T>
{
    T Instance { get; }
}

And change your partial class accordingly:

public partial class Class : Interface<Class> { }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!