使自动生成的分部类实现自定义接口

发布于 2024-12-03 02:21:03 字数 1562 浏览 1 评论 0原文

最近我一直在开发一个 Silverlight 项目,其中有一个自动生成的类位于 *File.Web.g.cs 中,其定义为:

[DataContract(Namespace="http://schemas.datacontract.org/2004/07/Project.Web.Models")]
public sealed partial class SwitchDevice : Entity
{
        /// <summary>
        /// Gets or sets the 'ID' value.
        /// </summary>
        [DataMember()]
        [Editable(false, AllowInitialValue=true)]
        [Key()]
        [RoundtripOriginal()]
        public int ID
        {
            get
            {
                return this._id;
            }
            set
            {
                if ((this._id != value))
                {
                    this.OnIDChanging(value);
                    this.ValidateProperty("ID", value);
                    this._id = value;
                    this.RaisePropertyChanged("ID");
                    this.OnIDChanged();
                }
            }
        }
}

我想做的是制作 SwitchDevice 实现 IDevice 接口。代码位于另一个名为 IDevice.cs 的文件中。我决定像这样扩展分部类:

namespace Project.Web.Models
{
  public interface IDevice
  {
      int ID
      {
          get;
          set;
      }
   }

  public partial class SwitchDevice : IDevice
  {
  }
}

出于某种原因,VS2010 看不到位于 Web.G.CS 文件中的定义并生成错误:

“Project.Web.Models.SwitchDevice”未实现接口 成员 'Project.Web.Models.IDevice.ID' PATH\Project\Interfaces\ISwitchDevice.cs

我浏览并阅读了很多网页,但我还没有真正发现类似的问题。也许我只是做错了,我不是专家。

有可能让这项工作成功吗?预先感谢您的任何提示和帮助!

Lately I've been working on a Silverlight project in which there is an auto generated class located in *File.Web.g.cs with a definition:

[DataContract(Namespace="http://schemas.datacontract.org/2004/07/Project.Web.Models")]
public sealed partial class SwitchDevice : Entity
{
        /// <summary>
        /// Gets or sets the 'ID' value.
        /// </summary>
        [DataMember()]
        [Editable(false, AllowInitialValue=true)]
        [Key()]
        [RoundtripOriginal()]
        public int ID
        {
            get
            {
                return this._id;
            }
            set
            {
                if ((this._id != value))
                {
                    this.OnIDChanging(value);
                    this.ValidateProperty("ID", value);
                    this._id = value;
                    this.RaisePropertyChanged("ID");
                    this.OnIDChanged();
                }
            }
        }
}

What I wanted to do is to make SwitchDevice implement the IDevice interface. Code is located in another file named IDevice.cs. I decided that I will extend the partial class like this:

namespace Project.Web.Models
{
  public interface IDevice
  {
      int ID
      {
          get;
          set;
      }
   }

  public partial class SwitchDevice : IDevice
  {
  }
}

For some reason VS2010 does not see the definition located in Web.G.CS file and generates error:

'Project.Web.Models.SwitchDevice' does not implement interface
member
'Project.Web.Models.IDevice.ID' PATH\Project\Interfaces\ISwitchDevice.cs

I've browsed and read a lot of web pages but I haven't really found a similar issue. Maybe I'm just doing it wrong, I'm not an expert.

Is it even possible to make this work? Thank you in advance for any tips and help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

帅哥哥的热头脑 2024-12-10 02:21:03

你尝试做的事情很好,我一直这样做。

确保您在同一命名空间下和同一项目内都有两个部分类定义文件。您使用该类实现的接口可能会在另一个程序集中,或者您会失去部分优势,并且必须从调用代码中引用您的 dal,这是您设计可以为您提供的一件事;与 DAL 隔离并针对接口进行编码。

what you try to do is fine and i do it all the times.

Make sure you have both partial class definition files under same namespace AND inside the same project. the interface you are implementing with that class will be probably in another assembly or you lose part of the advantages and will have to reference your dal anyway from the calling code and this is one thing you design could give you; isolation from DAL and coding against interfaces.

橘虞初梦 2024-12-10 02:21:03

很可能,自动生成的类 SwitchDevicesealed 类)不像类 Project.Web.Models 那样位于命名空间 Project.Web.Models 中。 编写的代码>SwitchDevice是。

为了使分部类按您的预期工作,类定义的所有部分都应位于同一命名空间中。否则,您将在不同的命名空间中拥有两个具有相同名称的(不相关的)类,并且它们中的 partial 修饰符不会发挥任何作用。

如果这是真的,您需要做的是将您自己的类片段移动到与自动生成的类相同的命名空间中,例如:

namespace Project.Web.Models
{
    public interface IDevice
    {
        int ID { get; set; }
    }
}

namespace Some.Namespace // copy the name from the auto-generated file
{
    public partial class SwitchDevice : Project.Web.Models.IDevice
    {
    }
}

In all likelihood, the auto-generated class SwitchDevice (the sealed one) is not in the namespace Project.Web.Models like the class SwitchDevice that you wrote is.

For partial classes to work as you expect, all parts of the class definition should be in the same namespace. Otherwise, you just have two (unrelated) classes with the same name in different namespaces, and the partial modifier in both of them does not play any role.

If this is true, what you need to do is move your own class fragment into the same namespace as the auto-generated class, e.g.:

namespace Project.Web.Models
{
    public interface IDevice
    {
        int ID { get; set; }
    }
}

namespace Some.Namespace // copy the name from the auto-generated file
{
    public partial class SwitchDevice : Project.Web.Models.IDevice
    {
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文