Problem with interface implementation in partial classes

后端 未结 2 1224
北海茫月
北海茫月 2020-12-11 04:01

I have a question regarding a problem with L2S, Autogenerated DataContext and the use of Partial Classes. I have abstracted my datacontext and for every table I use, I\'m im

相关标签:
2条回答
  • 2020-12-11 04:39

    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;
            }
         }
      }
    }
    
    0 讨论(0)
  • 2020-12-11 04:52

    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> { }
    
    0 讨论(0)
提交回复
热议问题