使用接口实例调用泛型方法
Calling a generic method with interface instances
作为
的后续问题
public interface IFeature { }
public class FeatureA : IFeature { }
IFeature a = new FeatureA();
Activate(a);
private static void Activate<TFeature>(TFeature featureDefinition) where TFeature : IFeature
{
}
我明白,一旦将 FeatureA
强制转换为 IFeature
,泛型方法将始终获得 IFeature
作为类型参数。
我们有一项服务为我们提供列表功能 (List<IFeature>
)。如果我们想遍历这些特性,在泛型方法中传递每个特性,我想除了
之外没有办法在泛型方法中获取具体类型
- 使用反射
- 使用动态变量确定运行时的类型(Calling a generic method with the correct derived type)
由于反射非常昂贵,我想使用动态转换。以这种方式调用该方法有什么缺点吗?不知怎的,当我这样做的时候我觉得很脏:-)
假设您可以修改您的代码库,您可以按如下方式使用访问者模式。否则,使用动态。
public interface IFeature
{
void Accept(Visitior visitor);
}
public class FeatureA : IFeature
{
public void Accept(Visitior visitor)
{
visitor.Visit(this);
}
}
public class FeatureB : IFeature
{
public void Accept(Visitior visitor)
{
visitor.Visit(this);
}
}
public class Visitior
{
public void Visit<TFeature>(TFeature feature) where TFeature : IFeature
{
Console.WriteLine(typeof(TFeature) == feature.GetType());//True
}
}
static void Main(string[] args)
{
List<IFeature> features = new List<IFeature>
{
new FeatureA(),
new FeatureB()
};
Visitior visitor = new Visitior();
foreach (var item in features)
{
item.Accept(visitor);
}
}
您可以使用 typeof 获取泛型/(非泛型)类型的类型对象:
public static T Parse<T>(String value)
{
object result = default(T);
var typeT = typeof (T);
if (typeT == typeof(Guid))
{
result = new Guid(value);
}
else if (typeT == typeof(TimeSpan))
{
result = TimeSpan.Parse(value);
}
else
{
result = Convert.ChangeType(value, typeT);
}
return (T)result;
}
我的简单方法returns T。而且这是一个关键点。它必须是通用的,以允许开发人员指定 return 类型。如果方法不 return 通用并且只接受一个,那么有几个理由让它成为通用的。为了避免对方法参数进行 box/unbox 操作,或者解决方法采用不同类型的参数而不是从公共基础 class/interface 继承的情况。这不是你的情况。所以代码中的方法不必是通用的。只需将您的参数键入 IFeature 并使用 is/as/GetType():
private static void Activate(IFeature feature)
{
if (feature is FeatureImplementationA)
{
//Do something...
}
}
作为
public interface IFeature { }
public class FeatureA : IFeature { }
IFeature a = new FeatureA();
Activate(a);
private static void Activate<TFeature>(TFeature featureDefinition) where TFeature : IFeature
{
}
我明白,一旦将 FeatureA
强制转换为 IFeature
,泛型方法将始终获得 IFeature
作为类型参数。
我们有一项服务为我们提供列表功能 (List<IFeature>
)。如果我们想遍历这些特性,在泛型方法中传递每个特性,我想除了
- 使用反射
- 使用动态变量确定运行时的类型(Calling a generic method with the correct derived type)
由于反射非常昂贵,我想使用动态转换。以这种方式调用该方法有什么缺点吗?不知怎的,当我这样做的时候我觉得很脏:-)
假设您可以修改您的代码库,您可以按如下方式使用访问者模式。否则,使用动态。
public interface IFeature
{
void Accept(Visitior visitor);
}
public class FeatureA : IFeature
{
public void Accept(Visitior visitor)
{
visitor.Visit(this);
}
}
public class FeatureB : IFeature
{
public void Accept(Visitior visitor)
{
visitor.Visit(this);
}
}
public class Visitior
{
public void Visit<TFeature>(TFeature feature) where TFeature : IFeature
{
Console.WriteLine(typeof(TFeature) == feature.GetType());//True
}
}
static void Main(string[] args)
{
List<IFeature> features = new List<IFeature>
{
new FeatureA(),
new FeatureB()
};
Visitior visitor = new Visitior();
foreach (var item in features)
{
item.Accept(visitor);
}
}
您可以使用 typeof 获取泛型/(非泛型)类型的类型对象:
public static T Parse<T>(String value)
{
object result = default(T);
var typeT = typeof (T);
if (typeT == typeof(Guid))
{
result = new Guid(value);
}
else if (typeT == typeof(TimeSpan))
{
result = TimeSpan.Parse(value);
}
else
{
result = Convert.ChangeType(value, typeT);
}
return (T)result;
}
我的简单方法returns T。而且这是一个关键点。它必须是通用的,以允许开发人员指定 return 类型。如果方法不 return 通用并且只接受一个,那么有几个理由让它成为通用的。为了避免对方法参数进行 box/unbox 操作,或者解决方法采用不同类型的参数而不是从公共基础 class/interface 继承的情况。这不是你的情况。所以代码中的方法不必是通用的。只需将您的参数键入 IFeature 并使用 is/as/GetType():
private static void Activate(IFeature feature)
{
if (feature is FeatureImplementationA)
{
//Do something...
}
}