【发布时间】:2018-11-10 23:39:57
【问题描述】:
从How to reflect an interfaced type<t> at runtime 开始,我有一个类型的实例,我知道它是从基本类型 DataPointProcessorBase 继承的 这个基类比较简单
public abstract class DataPointProcessorBase<T> : IDataPointProcessor<T> where T : class, IDataPointInput, new()
{
public abstract DataPointOutputBase GetOutput(T input);
}
Age_Input 实现接口并设置 Age_Processor 接收它
public class Age_Input : DataPointInputBase, IDataPointInput
{
public int AgeExact { get; set; }
}
public class Age_Processor : DataPointProcessorBase<Age_Input>
{
...
}
使用反射,我已经完成了正确投射的一半,所以我可以调用 GetOutput()
任何想法为什么我不能进入下面的第一个 if 语句?
var instance = Activator.CreateInstance(type);
if (instance is IDataPointProcessor<IDataPointInput>)//why can I not cast interface here?
{
//false
}
if (instance is IDataPointProcessor<Age_Input>)//hard-coded - works fine
{
var processor = instance as IDataPointProcessor<Age_Input>;
Age_Input temp = item as Age_Input;
if (temp is IDataPointInput)
{
//also true
}
var result = processor.GetOutput(temp);
}
【问题讨论】:
标签: c# generics reflection interface