更新:我的“问题”是由于对我正在使用反射的类的错误假设。我下面的问题是荒谬的,因为它不是那样做的。但我将它留在这里以防代码片段有用。
我正在使用反射从驻留在单独 程序集中的已编译类生成 View 模型。大多数一切都在工作,除了我从编译的程序集返回的类型显示一个接口(interface)而不是作为该接口(interface)的具体实现的类。
例如,在编译后的程序集中,我定义了一个如下属性:
public List<String> strings { get; set; }
使用反射,我已经能够生成如下所示的相应属性(和完整的类):
public ICollection<String> strings { get; set; }
我得到了这样做的类型(为简洁起见被严重删减):
Type value = <...some passed in type...>
String types = String.Join(",", value.GetGenericArguments().Select(t => t.Name).ToArray());
String.Format("{0}<{1}>", value.GetGenericTypeDefinition(), types);
有什么方法可以使用 Type 对象来确定返回的接口(interface)使用了哪个具体实现?我猜不会,但我想我会问。
如果没有办法,我正在考虑使用“最可能的实现”来查找给定的接口(interface)(即你传入“ICollection”,我将其转换为具体的“列表”)除非有更好的建议???
不一定需要“完美”的解决方案,因为它用于使用 T4 模板生成 Controller 、 View 和 View 模型(带有自动映射器和 Kendo UI)。所以如果有一些东西需要清理,那很好。所以欢迎任何让我接近的建议。
好吧,我玩了一下反射,这里是我带来的东西:
//Initial list with ints
var items = new List<int> {1, 2, 3, 4, 5};
//ICollection<int> reference
var collectionOfItems = (ICollection<int>) items;
//...
//First of, get collection Type object
var collectionType = collectionOfItems.GetType();
//Next, let's grab generic arguments
var genericArguments = collectionType.GetGenericArguments();
//For each generic candidate type we need to construct it with collection's generic arguments
var candidate = typeof(List<>).MakeGenericType(genericArguments);
//Perform check
if (collectionType.IsAssignableFrom(candidate))
Console.WriteLine("Can be casted to {0}", candidate);
else
Console.WriteLine("Cannot be cated to {0}", candidate);
是的,反射是可能的,但是你需要检查很多可能的类型(一些非 BCL 类型也可以实现 ICollection
)。最好只声明具有具体类型的属性
我是一名优秀的程序员,十分优秀!