Maf*_*fii 6 .net c# inheritance
我在EntityFramework的DbSet类中找到以下代码片段:
public new Type GetType()
{
return base.GetType();
}
Run Code Online (Sandbox Code Playgroud)
我不知道为什么隐藏了基本方法,所有基类都实现了调用base的方法.
这是object.GetType():
[SecuritySafeCritical]
[__DynamicallyInvokable]
[MethodImpl(MethodImplOptions.InternalCall)]
public extern Type GetType();
Run Code Online (Sandbox Code Playgroud)
这是在DbQuery课堂上:
/// <inheritdoc />
[EditorBrowsable(EditorBrowsableState.Never)]
[SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")]
public new Type GetType()
{
return base.GetType();
}
Run Code Online (Sandbox Code Playgroud)
这是在DbSet(DbSet<TEntity> : DbQuery<TEntity>)类中:
/// <inheritdoc />
[SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")]
[EditorBrowsable(EditorBrowsableState.Never)]
public new Type GetType()
{
return base.GetType();
}
Run Code Online (Sandbox Code Playgroud)
为什么或何时使用new关键字然后调用基本实现?
让我们考虑一下替代方案。
不要覆盖或新建任何内容
问题:没有可应用属性的代码。
只需覆盖它
问题:GetType不是虚拟的。
所以你只能使用new调用base.
真正的问题是,到底是什么如此重要,[EditorBrowsable(EditorBrowsableState.Never)]以至于他们不辞辛劳?我唯一的猜测是,他们认为开发人员会GetType与ElementTypeintellisense 混淆。
| 归档时间: |
|
| 查看次数: |
129 次 |
| 最近记录: |