使用反射获取泛型IDictionary的值

编程入门 行业动态 更新时间:2024-10-21 03:38:56
本文介绍了使用反射获取泛型IDictionary的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个实现 IDictionary< T,K> 的实例,我不知道编译时的T和K,并且想从中获取所有元素。我不想使用 IEnumerable ,这是由 IDictionary 实现的唯一非泛型接口。 。

代码到目前为止:

//获取类型 Type iDictType = instance.GetType()。GetInterface(IDictionary`2); 类型keyType = iDictType.GetGenericArguments()[0]; Type valueType = iDictType.GetGenericArguments()[1]; //获取键 IEnumerable键=(IEnumerable)dictType.GetProperty(Keys) .GetValue(instance,null); foreach(键中的对象键) { // ==>这不起作用:调用[]运算符对象value = dictType.GetProperty(Item) .GetValue(instance,new object [] {key}); //从TryGet获得另一个实例的值 MethodInfo tryGetValue = iDictType.GetMethod(TryGetValue); object [] arguments = new object [] {key,null}; bool hasElement =(bool)tryGetValue.Invoke(otherInstance,arguments); object anotherValue = arguments [1]; }

我也可以调用TryGetValue,但我认为应该可以调用[]运算符。任何人都可以帮助我吗?

解决方案

TKey / TValue ,并通过 MakeGenericMethod 切换到常规代码 - 如下所示:

(编辑) - 您也可以将 otherInstance 作为参数传递给它们)

静态类程序 { static void Main() { object obj = new Dictionary< int,string> { {123,abc},{456,def}}; foreach(类型iType在obj.GetType().GetInterfaces()) { if(iType.IsGenericType&&& iType.GetGenericTypeDefinition() == Typeof(IDictionary<,>)) { typeof(Program).GetMethod(ShowContents) .MakeGenericMethod(iType.GetGenericArguments()) .Invoke (null,new object [] {obj}); 休息; $ b public static void ShowContents< TKey,TValue>( IDictionary< TKey,TValue> data) { foreach(var对中的数据) { Console.WriteLine(pair.Key +=+ pair.Value); } } }

I have an instance that implements IDictionary<T, K>, I don't know T and K at compiletime, and want to get all elements from it. I don't want to use IEnumerable for some reason, which would be the only non-generic interface implemented by IDictionary.

Code I have so far:

// getting types Type iDictType = instance.GetType().GetInterface("IDictionary`2"); Type keyType = iDictType.GetGenericArguments()[0]; Type valueType = iDictType.GetGenericArguments()[1]; // getting the keys IEnumerable keys = (IEnumerable)dictType.GetProperty("Keys") .GetValue(instance, null); foreach (object key in keys) { // ==> this does not work: calling the [] operator object value = dictType.GetProperty("Item") .GetValue(instance, new object[] {key } ); // getting the value from another instance with TryGet MethodInfo tryGetValue = iDictType.GetMethod("TryGetValue"); object[] arguments = new object[] { key, null }; bool hasElement = (bool)tryGetValue.Invoke(otherInstance, arguments); object anotherValue = arguments[1]; }

I could also call TryGetValue, but I think it should be possible to call the [] operator. Can anybody help me?

解决方案

It would be better to figure out the TKey / TValue, and switch into regular code via MakeGenericMethod - like so:

(edit - you could pass in the otherInstance as an argument too, if they are of the same type)

static class Program { static void Main() { object obj = new Dictionary<int, string> { { 123, "abc" }, { 456, "def" } }; foreach (Type iType in obj.GetType().GetInterfaces()) { if (iType.IsGenericType && iType.GetGenericTypeDefinition() == typeof(IDictionary<,>)) { typeof(Program).GetMethod("ShowContents") .MakeGenericMethod(iType.GetGenericArguments()) .Invoke(null, new object[] { obj }); break; } } } public static void ShowContents<TKey, TValue>( IDictionary<TKey, TValue> data) { foreach (var pair in data) { Console.WriteLine(pair.Key + " = " + pair.Value); } } }

更多推荐

使用反射获取泛型IDictionary的值

本文发布于:2023-11-16 23:58:08,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:反射   IDictionary

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!