[英]C# Reflection - How can I tell if object o is of type KeyValuePair and then cast it?
I'm currently trying to write a Dump() method from LinqPad equivalent iin C# for my own amusment. 我目前正在尝试从LinqPad等效的iin C#编写Dump()方法来实现自己的娱乐。 I'm moving from Java to C# and this is an exercise rather than a business requirement. 我正在从Java迁移到C#,这是一项练习,而不是一项业务要求。 I've got almost everything working except for Dumping a Dictionary. 除转储字典外,我几乎所有工作正常。
The problem is that KeyValuePair is a Value type. 问题在于KeyValuePair是一个值类型。 For most other Value types I simply call the ToString method but this is insufficient as the KeyValuePair may contain Enumerables and other objects with undesirable ToString methods. 对于大多数其他Value类型,我仅调用ToString方法,但这是不够的,因为KeyValuePair可能包含Enumerables和其他具有不理想的ToString方法的对象。 So I need to work out if it's a KeyValuePair and then cast it. 因此,我需要弄清楚它是否是KeyValuePair,然后进行转换。 In Java I could use wildcard generics for this but I don't know the equivalent in C#. 在Java中,我可以为此使用通配符泛型,但我不知道C#中的等效项。
Your quest, given an object o, determine if it's a KeyValuePair and call Print on its key and value. 给定对象o,您的任务将确定其是否为KeyValuePair,并对其键和值调用Print。
Print(object o) {
...
}
Thanks! 谢谢!
If you don't know the types stored in the KeyValuePair
you need to exercise a bit of reflection code. 如果您不知道KeyValuePair
存储的类型,则需要执行一些反射代码。
Let's look at what is needed: 让我们看一下需要什么:
First, let's ensure the value isn't null
: 首先,让我们确保该值不为null
:
if (value != null)
{
Then, let's ensure the value is generic: 然后,确保该值是通用的:
Type valueType = value.GetType();
if (valueType.IsGenericType)
{
Then, extract the generic type definition, which is KeyValuePair<,>
: 然后,提取通用类型定义,即KeyValuePair<,>
:
Type baseType = valueType.GetGenericTypeDefinition();
if (baseType == typeof(KeyValuePair<,>))
{
Then extract the types of the values in it: 然后提取其中的值的类型:
Type[] argTypes = baseType.GetGenericArguments();
Final code: 最终代码:
if (value != null)
{
Type valueType = value.GetType();
if (valueType.IsGenericType)
{
Type baseType = valueType.GetGenericTypeDefinition();
if (baseType == typeof(KeyValuePair<,>))
{
Type[] argTypes = baseType.GetGenericArguments();
// now process the values
}
}
}
If you've discovered that the object does indeed contain a KeyValuePair<TKey,TValue>
you can extract the actual key and value like this: 如果发现该对象确实包含KeyValuePair<TKey,TValue>
,则可以提取实际的键和值,如下所示:
object kvpKey = valueType.GetProperty("Key").GetValue(value, null);
object kvpValue = valueType.GetProperty("Value").GetValue(value, null);
Presuming you are using the generic KeyValuePair then you need probably want to test for a particular instantiation, such as one created using a string for both key and value: 假设您使用的是通用KeyValuePair,那么您可能需要测试特定的实例化,例如使用键和值的字符串创建的实例化:
public void Print(object o)
{
if (o == null)
return;
if (o is KeyValuePair<string, string>)
{
KeyValuePair<string, string> pair = (KeyValuePair<string, string>)o;
Console.WriteLine("{0} = {1}", pair.Key, pair.Value);
}
}
If you want to test for any type of KeyValuePair then you'll need to use reflection. 如果要测试任何类型的KeyValuePair,则需要使用反射。 Do you? 你呢?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.