gpt4 book ai didi

c# - LINQ2SQL 中用于共享常用方法的抽象类

转载 作者:太空宇宙 更新时间:2023-11-03 22:20:03 24 4
gpt4 key购买 nike

我在尝试实现由 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 之外,我还能在这里做什么?谢谢

最佳答案

它没有作为override 实现,因此不算数。但是,隐式接口(interface)实现确实很重要,所以这是可行的:

partial class DirectorPoll : IGenericPoll {}
partial class StudentPoll : IGenericPoll {}
public interface IGenericPoll
{
bool Completed { get; set; }
bool? Reopen { get; set; }
}
public static class GenericPoll {
public static bool CanEdit(this IGenericPoll instance)
{
return !instance.Completed || instance.Reopen.GetValueOrDefault();
}
}

关于c# - LINQ2SQL 中用于共享常用方法的抽象类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3461894/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com