C#反思 - 我怎样才能知道,如果对象o是一个类型KeyValuePair的再投呢? [英] C# Reflection - How can I tell if object o is of type KeyValuePair and then cast it?
问题描述
我目前正在写从LinqPad相当于IIN C#转储()方法,为我自己amusment。我从Java移动到C#这是一个锻炼,而不是一个业务需求。我已经得到了几乎所有的东西,除了倾倒解释工作。
的问题是,KeyValuePair是一个值类型。对于大多数其他值类型我只需调用ToString方法,但这是不够的KeyValuePair可能含有可枚举等物与不良的ToString方法。所以,我需要工作,如果它是一个KeyValuePair再投了。在Java中,我可以使用这个通配符仿制药,但我不知道C#中的等价物。
您的追求,给定一个对象o,确定它是否是一个KeyValuePair和呼叫其键和值打印。
打印(对象o){
...
$} b $ b
谢谢!
如果您不知道存储在 KeyValuePair
你需要锻炼一点反射代码。
$ b类型$ b
让我们看看需要什么:
首先,让我们确保该值不是空
如果
(值!= NULL) pre>
{
然后,让我们确保值是通用的:
键入VALUETYPE = value.GetType();
如果(valueType.IsGenericType)
{
然后,提取一般类型顾名思义,这是
KeyValuePair<,>
:键入BASETYPE = valueType.GetGenericTypeDefinition();
如果(BASETYPE == typeof运算(KeyValuePair<,>))
{
然后提取类型值的吧:
键入[] = argTypes baseType.GetGenericArguments();
最终代码:
如果(!值= NULL)
{
类型的ValueType = value.GetType();
如果(valueType.IsGenericType)
{
型BASETYPE = valueType.GetGenericTypeDefinition();
如果(BASETYPE == typeof运算(KeyValuePair<,>))
{
类型[] = argTypes baseType.GetGenericArguments();
//现在处理的值
}
}
}
如果您发现该对象确实包含一个
KeyValuePair< TKEY的,TValue>
您可以提取像这样的实际键和值:对象kvpKey = valueType.GetProperty(密钥)的GetValue(值null)。
对象kvpValue = valueType.GetProperty(值)的GetValue(值null)。
I'm currently trying to write a Dump() method from LinqPad equivalent iin C# for my own amusment. I'm moving from Java to C# and this is an exercise rather than a business requirement. I've got almost everything working except for Dumping a Dictionary.
The problem is that KeyValuePair is a Value type. 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. So I need to work out if it's a KeyValuePair and then cast it. In Java I could use wildcard generics for this but I don't know the equivalent in C#.
Your quest, given an object o, determine if it's a KeyValuePair and call Print on its key and value.
Print(object o) { ... }
Thanks!
解决方案If you don't know the types stored in the
KeyValuePair
you need to exercise a bit of reflection code.Let's look at what is needed:
First, let's ensure the value isn't
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<,>
: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:object kvpKey = valueType.GetProperty("Key").GetValue(value, null); object kvpValue = valueType.GetProperty("Value").GetValue(value, null);
这篇关于C#反思 - 我怎样才能知道,如果对象o是一个类型KeyValuePair的再投呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!