重写生成的部分类中的虚拟方法
在我的 LINQ 生成类中,我有这个方法:
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Id",
AutoSync=AutoSync.OnInsert, DbType="Int NOT NULL IDENTITY",
IsPrimaryKey=true, IsDbGenerated=true)]
public int Id
{
get
{
return this._Id;
}
set
{
if ((this._Id != value))
{
this.OnIdChanging(value);
this.SendPropertyChanging();
this._Id = value;
this.SendPropertyChanged("Id");
this.OnIdChanged();
}
}
}
在我的编码部分类中,我从基类扩展。基类定义:
public virtual int Id { get; set; }
这个概念是我可以在基类级别添加 Equals 和 Hashcode,以及在基类级别添加一些其他功能。问题是生成的类(从 LINQ-to-SQL)没有获得 override
关键字,因此它不起作用。如果它有override
,我就准备好了。
请建议如何完成此操作。
In my LINQ generate class, I have this method:
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Id",
AutoSync=AutoSync.OnInsert, DbType="Int NOT NULL IDENTITY",
IsPrimaryKey=true, IsDbGenerated=true)]
public int Id
{
get
{
return this._Id;
}
set
{
if ((this._Id != value))
{
this.OnIdChanging(value);
this.SendPropertyChanging();
this._Id = value;
this.SendPropertyChanged("Id");
this.OnIdChanged();
}
}
}
In my coded partial class, I extend from a base class. The base class defines:
public virtual int Id { get; set; }
The concept is that I can add the Equals and Hashcode at the base class level, as well as some other functionality at the base class level. The problem is that the generated classes (from LINQ-to-SQL) do not get an override
keyword so it doesnt work. If it had override
I would be all set.
Please suggest on how to complete this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使基类抽象,然后 Id 属性变为
生成的代码将实现该抽象属性。
You could make the base class
abstract
, and then the Id property becomesThe generated code will implement that abstract property.
分部类与覆盖类不同。您只是说的是部分课程。好的,我有 1 个类,但我已将源代码拆分为多个文件(主要是出于可维护的原因,例如将自动生成的代码与您自己的代码分开)。 C# 编译器只是将分部类的每个部分合并在一起,并将其作为 1 个类进行威胁。当然,覆盖一个类意味着您创建一个具有基类特征的新类。这是非常不同的。
现在可以指导 EF 如何生成代码(假设您正在使用 EF),但相对容易。不过,您可以做的一个选择是更改基类,使其不使用属性 Id,而是使用更容易识别(并且更具体?)的名称,例如“CompareIdentifier”。这样您就可以在派生类中重写 CompareIdentifier 以返回自动生成的 ID。当然,所有的比较工作都针对 CompareIdentifier
A partial class is different from overriding a class. With a partial class you're just saying. Ok, i have 1 class but i have splitted the source up over multiple files (mainly for maintainable reasons like splitting the auto generated code from your own code). The C# compiler just merges each part of a partial class together and threats it as 1 class. Ofcourse overriding a class means you create a new class which takes on the characteristics of the base class. That is very different.
Now it's possible but not relatively easy to direct EF of how to generate your code (assuming you are using EF). One option you can do though is to change your base class not to work with the Property Id but a more recognizable (and specifix?) name like 'CompareIdentifier'. With that you can override CompareIdentifier in your derived class for returning the auto generated ID. And ofcourse do all comparison work against CompareIdentifier
我会围绕这个创建一个包装类。
I would have created a wrapper class around this.