部分类中的接口实现问题 [英] Problem with interface implementation in partial classes

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

问题描述

我有一个关于 L2S、自动生成的 DataContext 和部分类的使用问题的问题.我已经抽象了我的数据上下文,对于我使用的每个表,我正在实现一个带有接口的类.在下面的代码中,您可以看到我有接口和两个部分类.第一个类只是为了确保自动生成的数据上下文中的类继承接口.另一个自动生成的类确保实现接口中的方法.

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 implementing a class with an interface. In the code below you can see I have the Interface and two partial classes. The first class is just there to make sure the class in the auto-generated datacontext inherets Interface. The other autogenerated class makes sure the method from Interface is implemented.

namespace PartialProject.objects
{

public interface Interface
{
    Interface Instance { get; }
}

//To make sure the autogenerated code inherits Interface
public partial class Class : Interface { }

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

}

现在我的问题是在自动生成的类中实现的方法给出了以下错误:-> 属性实例"无法从接口PartialProject.objects.Interface"实现属性.类型应该是PartialProjects.objects.Interface".<-

Now my problem is that the method implemented in the autogenerated class gives the following error: -> Property 'Instance' cannot implement property from interface 'PartialProject.objects.Interface'. Type should be 'PartialProjects.objects.Interface'. <-

知道如何解决此错误吗?请记住,我无法编辑自动生成的代码中的任何内容.

Any idea how this error can be resolved? Keep in mind that I can't edit anything in the autogenerated code.

提前致谢!

推荐答案

你可以通过显式实现接口来解决这个问题:

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;
        }
     }
  }
}

这篇关于部分类中的接口实现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆