gpt4 book ai didi

c# - 如何在 C# 中使用反射向字典添加值?

转载 作者:行者123 更新时间:2023-11-30 13:51:03 25 4
gpt4 key购买 nike

我有以下词典:

private Dictionary<string, double> averages = new Dictionary<string, double>();

现在我想使用反射来添加两个额外的值。我可以检索字段信息,但我还需要做什么?

FieldInfo field = ProjectInformation.SourceManager.GetType().GetField("averages");
if (field != null)
{
//what should be here?
}

最佳答案

MethodInfo mi = field.FieldType.GetMethodInfo("set_Item");
Object dict = field.GetValue(ProjectInformation.SourceManager);
mi.Invoke(dict, new object[] {"key", 0.0} );

关于c# - 如何在 C# 中使用反射向字典添加值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5161459/

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