运行 将一个类型转换(向下转换)到另一个子类型时出现时间错误
run time error when Casting (down casting) a type to another subType
在我创建的许多其他类型中,可以向下转换类型
而且我通常也会创建一个扩展方法,这样会更容易管理...
BaseTypeM
BTDerV : BaseTypeM
BTDerLastDescndnt : BTDerV
现在我创建一个 LastDerived 类型并将其值分配给 ParentType
BTDerV BTDer;
BTDerLastDescndnt BTDerLastDesc = new BTDerLastDescndnt(parA, ParB);
this.BTDer = BTDerLastDesc;
然后使用 downCast Extension
var LDesc = this.BTDer.AsBTDerLastDescndnt();
实际上是
public static BTDerLastDescndnt AsBTDerLastDescndnt(this BTDerV SelfBTDerV )
{
return (BTDerLastDescndnt)SelfBTDerV;
}
现在当我按照下面的代码执行此操作时,它确实可以编译但给我一个 运行-time 错误
//BTDerV---v v---BaseTypeM
public class SqlParDefV : SqlParameterM
{
public override SqlSpParDefMeta ParDMT
{
get {
return base.ParDMT;
}
set {
base.ParDMT = value;
}
}
public SqlParDefV(int bsprpOrdinal, string bsprpParName, MSSTypesS bdprpTypeS, bool bsprpIsDbStuctured, bool bsprpIsReq = true, ParameterDirection bsprpDirection = ParameterDirection.Input)
{
this.ParDMT = new SqlSpParDefMeta(bsprpOrdinal, bsprpParName, bdprpTypeS, bsprpIsReq, bsprpIsDbStuctured, bsprpDirection);
}
}
//BTDerLastDescndnt---v
public sealed class SqlParTvDrecDefinitionVScl : SqlParDefV
{
public override SqlSpParDefMeta ParDMT
{
get {
return base.ParDMT;
}
set {
base.ParDMT = value;
}
}
public SprocTvTargetSF.currentSDTObjType SqlObjType { get; set; }
public SqlMetaData[] Meta { get; set; }
public SqlParTvDrecDefinitionVScl(int bsprpOrdinal, string bsprpParName, SprocTvTargetSF.currentSDTObjType ctrSqlObjType, SqlMetaData[] parGeneratedSqlMetaData, MSSTypesS bdprpTypeS, bool bsprpIsDbStuctured, bool bsprpIsReq = true, ParameterDirection bsprpDirection = ParameterDirection.Input)
: base(bsprpOrdinal, bsprpParName, bdprpTypeS, bsprpIsDbStuctured, bsprpIsReq, bsprpDirection)
{
this.SqlObjType = ctrSqlObjType;
this.Meta = parGeneratedSqlMetaData;
}
}
这里有什么不寻常的地方吗?还是我很困惑并错过了一些基本规则?
我不确定从 Derived
到 MoreDerived
的转换在这里失败的确切原因。但是,一个潜在的解决方法(注意:可能是代码味道)是 as
运算符:
public static MoreDerived AsMoreDerived (this Derived d)
{
return d as MoreDerived;
}
请注意,as
有效地尝试转换和 returns null,因此您需要在那里进行适当的检查。
在我创建的许多其他类型中,可以向下转换类型 而且我通常也会创建一个扩展方法,这样会更容易管理...
BaseTypeM
BTDerV : BaseTypeM
BTDerLastDescndnt : BTDerV
现在我创建一个 LastDerived 类型并将其值分配给 ParentType
BTDerV BTDer;
BTDerLastDescndnt BTDerLastDesc = new BTDerLastDescndnt(parA, ParB);
this.BTDer = BTDerLastDesc;
然后使用 downCast Extension
var LDesc = this.BTDer.AsBTDerLastDescndnt();
实际上是
public static BTDerLastDescndnt AsBTDerLastDescndnt(this BTDerV SelfBTDerV )
{
return (BTDerLastDescndnt)SelfBTDerV;
}
现在当我按照下面的代码执行此操作时,它确实可以编译但给我一个 运行-time 错误
//BTDerV---v v---BaseTypeM
public class SqlParDefV : SqlParameterM
{
public override SqlSpParDefMeta ParDMT
{
get {
return base.ParDMT;
}
set {
base.ParDMT = value;
}
}
public SqlParDefV(int bsprpOrdinal, string bsprpParName, MSSTypesS bdprpTypeS, bool bsprpIsDbStuctured, bool bsprpIsReq = true, ParameterDirection bsprpDirection = ParameterDirection.Input)
{
this.ParDMT = new SqlSpParDefMeta(bsprpOrdinal, bsprpParName, bdprpTypeS, bsprpIsReq, bsprpIsDbStuctured, bsprpDirection);
}
}
//BTDerLastDescndnt---v
public sealed class SqlParTvDrecDefinitionVScl : SqlParDefV
{
public override SqlSpParDefMeta ParDMT
{
get {
return base.ParDMT;
}
set {
base.ParDMT = value;
}
}
public SprocTvTargetSF.currentSDTObjType SqlObjType { get; set; }
public SqlMetaData[] Meta { get; set; }
public SqlParTvDrecDefinitionVScl(int bsprpOrdinal, string bsprpParName, SprocTvTargetSF.currentSDTObjType ctrSqlObjType, SqlMetaData[] parGeneratedSqlMetaData, MSSTypesS bdprpTypeS, bool bsprpIsDbStuctured, bool bsprpIsReq = true, ParameterDirection bsprpDirection = ParameterDirection.Input)
: base(bsprpOrdinal, bsprpParName, bdprpTypeS, bsprpIsDbStuctured, bsprpIsReq, bsprpDirection)
{
this.SqlObjType = ctrSqlObjType;
this.Meta = parGeneratedSqlMetaData;
}
}
这里有什么不寻常的地方吗?还是我很困惑并错过了一些基本规则?
我不确定从 Derived
到 MoreDerived
的转换在这里失败的确切原因。但是,一个潜在的解决方法(注意:可能是代码味道)是 as
运算符:
public static MoreDerived AsMoreDerived (this Derived d)
{
return d as MoreDerived;
}
请注意,as
有效地尝试转换和 returns null,因此您需要在那里进行适当的检查。