更新
请求重新打开,因为其他 SO 答案没有解决方案,但对该问题的评论之一有一个我想接受的解决方案,因为它适用于该场景。
原始问题
我在使用选择适当扩展方法的非抽象基类和子类编写扩展方法时遇到问题。
我下面有一个非常简单的示例(从一个更大的项目中抽象出来),它使用扩展方法“Run”。预期输出列在每个类旁边的注释中。
public class Parent { }; // Should output "Parent"
public class ChildA : Parent { }; // Should output "Child A"
public class ChildB : Parent { }; // Should output "Parent"
// Expected Output: ChildA, Parent, Parent
public class Program
{
public static void Main()
{
var commands = new List<Parent>() { new ChildA(), new ChildB(), new Parent() };
Console.WriteLine(string.Join(", ", commands.Select(c => c.Run())));
}
}
这是我迄今为止的尝试,但必须有一种更简洁的方法来做到这一点:
- 无类型检查 - 导致仅使用 Parent 扩展方法(Parent、Parent、Parent)
- 显式类型检查 - 正确的输出,但必须显式检查每种扩展可能性(ChildA、Parent、Parent)的类型
- 尝试将.ChangeType 转换为动态类型 - 由于扩展无法捕获动态类型(无输出),因此出现运行时异常
- 尝试使用反射进行通用转换 - 尚未完全可操作,但不确定方法是否有效
尝试列表
public static class Extensions
{
public static string Run(this ChildA model)
{
return "ChildA";
}
public static string Run(this Parent model)
{
return model.Run1(); // Change to test different approaches
}
public static string Run1(this Parent model) // No type-checking
{
return "Parent";
}
public static string Run2(this Parent model) // Explicitly check sub-types
{
if (model is ChildA)
return ((ChildA)model).Run();
else
return "Parent";
}
public static string Run3(this Parent model) // Attempted dynamic type conversion
{
if (model.GetType().BaseType == typeof(Parent))
{
dynamic changedObj = Convert.ChangeType(model, model.GetType());
return changedObj.Run();
}
else
return "Parent";
}
public static string Run4(this Parent model) // Attempted reflected generic type conversion
{
if (model.GetType().BaseType == typeof(Parent))
{
var method = typeof(Extensions).GetMethod("Cast");
var generic = method.MakeGenericMethod(new[] { model.GetType() });
//var generic = generic.Invoke(new object(), null);
//return generic.Run();
return "Not working yet";
}
else
return "Parent";
}
public static T Cast<T>(this object input)
{
return (T) input;
}
}
最佳答案
为Parent
和ChildA
创建两个扩展方法,您可以使用dynamic
将关联移至运行时。
Console.WriteLine(string.Join(", ", commands.Select(c => Extensions.Run(c as dynamic))));
关于c# - 具有基类和子类的扩展方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30645441/