LINQ2SQL 中用于共享常用方法的抽象类
我在尝试在 linq2sql 设计器创建的两个类之间实现共享方法/属性时遇到问题。
我的两个类有两个主要属性(来自数据库模型):
public partial class DirectorPoll
{
public bool Completed {get; set;}
public bool? Reopen { get; set; }
//more properties
}
public partial class StudentPoll
{
public bool Completed {get; set;}
public bool? Reopen { get; set; }
//more properties
}
现在例如我创建一个抽象类:
public abstract class GenericPoll
{
public abstract bool Completed { get; set; }
public abstract bool? Reopen { get; set; }
public bool CanEdit
{
get
{
if (Completed == false) return true;
if (Reopen.GetValueOrDefault(false) == false) return false;
return true;
}
}
}
然后
public partial class DirectorPoll : GenericPoll
public partial class StudentPoll: GenericPoll
但是当我尝试编译时它说“Director 没有实现继承的抽象成员 GenericPoll.Completed.get”。但它就在那里。所以我认为我被迫对设计器自动生成的属性进行覆盖,但如果我稍后更新数据库并重新编译,它将给我同样的错误。
我想我可能在这里遗漏了一些东西,但我尝试了不同的方法但没有成功。那么,除了在每个部分类中实现 CanEdit 之外,我还能做什么呢?谢谢
I'm having trouble trying to implement a shared method/property between two classes created by the linq2sql designer.
My two classes have two main properties (coming from the db model):
public partial class DirectorPoll
{
public bool Completed {get; set;}
public bool? Reopen { get; set; }
//more properties
}
public partial class StudentPoll
{
public bool Completed {get; set;}
public bool? Reopen { get; set; }
//more properties
}
Now for example I create an abstract class:
public abstract class GenericPoll
{
public abstract bool Completed { get; set; }
public abstract bool? Reopen { get; set; }
public bool CanEdit
{
get
{
if (Completed == false) return true;
if (Reopen.GetValueOrDefault(false) == false) return false;
return true;
}
}
}
Then
public partial class DirectorPoll : GenericPoll
public partial class StudentPoll: GenericPoll
But when I try to compile it says "Director does not implement inherited abstract member GenericPoll.Completed.get". But it is there. So I think I'm forced to put an override to the property automatically generated by the designer, but if I update the database later and recompile it will give me the same error.
I think I might be missing something here but I've tried different approaches with no success. ¿So what can I do here, besides implementing CanEdit in each of my partial classes? Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它不是作为
覆盖
实现的,因此它不算数。然而,隐式接口实现确实计数,所以这是有效的:It isn't implemented as an
override
, so it doesn't count. However, implicit interface implementations do count, so this works:一种选择:创建一个包含
Completed
和Reopen
的接口,使类实现该接口(通过部分类的手动位),然后编写一个扩展方法来扩展该接口界面。我认为这应该可行:诚然,它必须是一种方法而不是属性,因为不存在扩展属性之类的东西,但这并不算太困难。
(我相信我在
CanEdit
中重构的逻辑是正确的。所有这些明确的对错都在困扰我;)One option: create an interface containing
Completed
andReopen
, make the classes implement the interface (via the manual bits of the partial classes), then write an extension method which extends that interface. I think that should work:Admittedly then it has to be a method rather than a property, as there's no such thing as an extension property, but that's not too much of a hardship.
(I believe my refactoring of your logic in
CanEdit
is correct. All those explicit trues and falses were doing my head in ;)