gpt4 book ai didi

c# - 如何在给定 Dictionary 类型的情况下获取 TKey 和 TValue 的类型

转载 作者:太空宇宙 更新时间:2023-11-03 15:49:28 28 4
gpt4 key购买 nike

我想在给定 Dictionary<TKey,TValue> 的情况下获取 TKey 和 TValue 的类型类型。

例如。如果类型是 Dictionary<Int32,String>我想知道如何获得keyType = typeof(Int32) 和valueType = typeof(字符串)

最佳答案

我认为这可能是您正在寻找的:

Dictionary<int, string> dictionary = new Dictionary<int, string>();
Type[] arguments = dictionary.GetType().GetGenericArguments();
Type keyType = arguments[0];
Type valueType = arguments[1];

引用:Type.GetGenericArguments

关于c# - 如何在给定 Dictionary<TKey,TValue> 类型的情况下获取 TKey 和 TValue 的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26587501/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com