使自动生成的分部类实现自定义接口
最近我一直在开发一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你尝试做的事情很好,我一直这样做。
确保您在同一命名空间下和同一项目内都有两个部分类定义文件。您使用该类实现的接口可能会在另一个程序集中,或者您会失去部分优势,并且必须从调用代码中引用您的 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.
很可能,自动生成的类
SwitchDevice
(sealed
类)不像类Project.Web.Models
那样位于命名空间Project.Web.Models
中。 你编写的代码>SwitchDevice是。为了使分部类按您的预期工作,类定义的所有部分都应位于同一命名空间中。否则,您将在不同的命名空间中拥有两个具有相同名称的(不相关的)类,并且它们中的
partial
修饰符不会发挥任何作用。如果这是真的,您需要做的是将您自己的类片段移动到与自动生成的类相同的命名空间中,例如:
In all likelihood, the auto-generated class
SwitchDevice
(thesealed
one) is not in the namespaceProject.Web.Models
like the classSwitchDevice
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.: