我如何检测的对象是一个泛型集合,它包含什么类型的? [英] How do I detect that an object is a generic collection, and what types it contains?
问题描述
我有一个字符串序列化的实用工具,需要的(几乎)所有类型的变量,并将其转换成一个字符串。因此,例如,根据我的约定,123的整数值将被序列为i:3:123。(ⅰ=整数; 3 =长度字符串; 123 =值)
I have a string serialization utility that takes a variable of (almost) any type and converts it into a string. Thus, for example, according to my convention, an integer value of 123 would be serialized as "i:3:123" (i=integer; 3=length of string; 123=value).
该实用程序处理所有的基本类型,以及一些非泛型集合一样的ArrayList和哈希表。接口的形式的
The utility handles all primitive type, as well as some non-generic collections, like ArrayLists and Hashtables. The interface is of the form
公共静态字符串StringSerialize(对象o){}
和内部检测我什么类型的对象,并相应地对其进行序列化。
and internally I detect what type the object is and serialize it accordingly.
现在我想升级我的程序来处理泛型集合。有趣的是,我无法找到一个合适的函数来检测的对象是一个泛型集合,它包含哪些类型 - 这两者的信息片段,我需要以正确序列化。至今我一直在使用形式编码
Now I want to upgrade my utility to handle generic collections. The funny thing is, I can't find an appropriate function to detect that the object is a generic collection, and what types it contains - both of which pieces of information I need in order to serialize it correctly. To date I've been using coding of the form
如果(邻为int){//做一些事情}
但似乎并没有泛型工作。
but that doesn't seem to work with generics.
你有什么建议?
编辑:感谢<一个href=\"http://stackoverflow.com/questions/755200/how-do-i-detect-that-an-object-is-a-generic-collection-and-what-types-it-contain/755217#755217\">Lucero,我已经得到接近答案,但我被困在这个小语法难题在这里:
Thanks to Lucero, I've gotten closer to the answer, but I'm stuck at this little syntactical conundrum here:
if (t.IsGenericType) {
if (typeof(List<>) == t.GetGenericTypeDefinition()) {
Type lt = t.GetGenericArguments()[0];
List<lt> x = (List<lt>)o;
stringifyList(x);
}
}
这code不能编译,因为 LT
是不允许的&LT; T&GT;
A 名单,LT的说法;&GT;
对象。为什么不?什么是正确的语法?
This code doesn't compile, because "lt
" is not allowed as the <T>
argument of a List<>
object. Why not? And what is the correct syntax?
推荐答案
重新您的难题;我假设 stringifyList
是一个通用的方法是什么?您需要使用反射来调用它:
Re your conundrum; I'm assuming stringifyList
is a generic method? You would need to invoke it with reflection:
MethodInfo method = typeof(SomeType).GetMethod("stringifyList")
.MakeGenericMethod(lt).Invoke({target}, new object[] {o});
其中, {目标}
是空
的静态方法,或这个
对当前实例的实例方法。
where {target}
is null
for a static method, or this
for an instance method on the current instance.
另外 - 我不认为所有的藏品是:根据列表&LT; T&GT;
,B:泛型类型。最重要的是:?他们实施的IList&LT; T&GT;
一些 T
Further - I wouldn't assume that all collections are a: based on List<T>
, b: generic types. The important thing is: do they implement IList<T>
for some T
?
下面是一个完整的例子:
Here's a complete example:
using System;
using System.Collections.Generic;
static class Program {
static Type GetListType(Type type) {
foreach (Type intType in type.GetInterfaces()) {
if (intType.IsGenericType
&& intType.GetGenericTypeDefinition() == typeof(IList<>)) {
return intType.GetGenericArguments()[0];
}
}
return null;
}
static void Main() {
object o = new List<int> { 1, 2, 3, 4, 5 };
Type t = o.GetType();
Type lt = GetListType(t);
if (lt != null) {
typeof(Program).GetMethod("StringifyList")
.MakeGenericMethod(lt).Invoke(null,
new object[] { o });
}
}
public static void StringifyList<T>(IList<T> list) {
Console.WriteLine("Working with " + typeof(T).Name);
}
}
这篇关于我如何检测的对象是一个泛型集合,它包含什么类型的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!