[英]Inherit from an abstract base with a more specific method
Lots of questions with these keywords; 这些关键字有很多问题; I sifted through the first 30 or so then gave up. 我筛选了前30个左右,然后放弃了。
I have interfaces like so: 我有这样的接口:
public interface IColumnRule
{
int Length { get; set; }
string Name { get; set; }
object Parse(string toParse);
int Position { get; set; }
Type Type { get; }
}
public interface IColumnRule<T> : IColumnRule
{
ColumnRule<T>.RuleFluentInterface<T> Configure { get; }
new T Parse(string rowdata);
Func<string, T> ParsingFunction { get; set; }
}
... the idea being, one would implement the IColumnRule<T>
to create a strongly-typed column parser utilizing the Func<string, T>
. ...的想法是,可以实现IColumnRule<T>
来使用Func<string, T>
创建一个强类型的列解析器。
The problem is, I store these IColumnRule<T>
concretes in an IList<IColumnRule>
container. 问题是,我将这些IColumnRule<T>
混凝土存储在IList<IColumnRule>
容器中。 There are multiple types of IColumnRule<T>
, each implementing on a different type. IColumnRule<T>
有多种类型,每种类型都实现不同的类型。 When I invoke the Parse
method on the IColumnRule
interface, I am expecting the new Parse(string)
method of the subclass to be called, but the base Parse
is the one actually being called. 当我在IColumnRule
接口上调用Parse
方法时,我期望将调用该子类的new Parse(string)
方法,但是基本Parse
是实际被调用的方法。
How can I invoke the subclass generic T Parse(string)
method from a collection of IColumnRule
using the interface's object Parse(string)
... or is this impossible? 如何使用接口的object Parse(string)
从IColumnRule
的集合中调用子类的通用T Parse(string)
方法...还是这不可能?
Your implementation of IColumnRule<T>
would have to provide a compliant Parse
method. 您的IColumnRule<T>
实现必须提供一个兼容的Parse
方法。 Given your code, the easiest way to do that is with an protected abstract method in your base class which is overridden in your subclass. 对于您的代码,最简单的方法是在基类中使用受保护的抽象方法,该方法在子类中被覆盖。
public abstract class ColumnRule : IColumnRule
{
...
public object Parse(string rowdata)
{
return this.ParseInternal(rowdata);
}
protected abstract object ParseInternal(rowdata);
}
public class ColumnRule<T> : ColumnRule, IColumnRule<T>
{
...
public new T Parse(string rowdata)
{
// strong-typed parse method
}
protected override object ParseInternal(string rowdata)
{
return this.Parse(rowdata); // invokes strong-typed method
}
}
When I invoke the Parse method on the IColumnRule interface, I am expecting the new Parse(string) method of the subclass to be called, but the base Parse is the one actually being called. 当我在IColumnRule接口上调用Parse方法时,我期望将调用该子类的新Parse(string)方法,但是基本Parse是实际被调用的方法。
Yes, it would. 是的,会的。 You've got two methods which are unrelated as far as the type system, CLR and compiler are concerned. 就类型系统(CLR和编译器)而言,您有两种无关的方法。
How can I invoke the subclass generic T Parse(string) method from a collection of IColumnRule using the interface's object Parse(string) ... or is this impossible? 如何使用接口的对象Parse(string)从IColumnRule的集合中调用子类的通用T Parse(string)方法...还是这不可能?
The simplest approach would be to have an abstract class implementing this as: 最简单的方法是让一个抽象类将其实现为:
public abstract ColumnRuleBase<T> : IColumnRule<T>
{
public object Parse(string toParse)
{
IColumnRule<T> genericThis = this;
return genericThis.Parse(toParse);
}
...
}
(If you had the two method names being different, it would be slightly simpler.) (如果两个方法的名称不同,那会稍微简单一些。)
You'd have to know the type somehow: 您必须以某种方式知道类型:
List<IColumnRule> rules; // populated from somewhere
foreach (IColumRule<int> rule in rules.OfType<IColumnRule<int>>()) {
int foo = rule.Parse(rowData);
}
Or just cast a known element: 或者只是投射一个已知元素:
int foo = ((IColumnRule<int>)rules[5]).Parse(rowData);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.