C# ContainsKey方法与变量的用法

C# ContainsKey方法与变量的用法,c#,json,dictionary,keyvaluepair,C#,Json,Dictionary,Keyvaluepair,我有一个字符串变量,它保存了一些值,我希望能够检查该字符串是否作为一个具有变量名的键存在于字典中。 如您在下面的代码中所看到的,以获得更清晰的理解 string searchDuration = "200"; var response = new Dictionary<string, string>() { {"searchDuration","200"

我有一个字符串变量,它保存了一些值,我希望能够检查该字符串是否作为一个具有变量名的键存在于字典中。 如您在下面的代码中所看到的,以获得更清晰的理解

        string searchDuration = "200";

        var response = new Dictionary<string, string>()
        {
            {"searchDuration","200"},
            {"minRssi", "-70"},
            {"optionalFilter","NO_FILTERS_ACTIVE_SCANNING"},
            {"txPowerLevel","200"},
            {"peripheralId","123wrong"}
        };
但我不(实际上不能)这样使用它,因为

  • 我需要动态地传递每个字符串变量,我不能将每个变量名都写为字符串来传递给ConstainsKey方法
  • 它只检查值,可能有多个“200”值,这种情况给我错误的结果
  • 我只想将值“200”与相关键“searchDuration”进行比较,而不是与具有相同值的“txPowerLevel”进行比较

是否有一种方法可以检查字符串变量是否作为字典中的键存在,以将其值与字典成员进行比较?

我建议采用这种方法:

string searchDuration = "200";

var response = new Dictionary<string, string>()
{
    {"searchDuration","200"},
    {"minRssi", "-70"},
    {"optionalFilter","NO_FILTERS_ACTIVE_SCANNING"},
    {"txPowerLevel","-16"},
    {"peripheralId","123wrong"}
};

var wasItThere = response.TryGetValue(nameof(searchDuration), out var value);
Console.WriteLine(wasItThere && (value == searchDuration));
string searchDuration=“200”;
var response=newdictionary()
{
{“搜索持续时间”,“200”},
{“minRssi”,“-70”},
{“optionalFilter”、“NO_FILTERS\u ACTIVE_SCANNING”},
{“txPowerLevel”,“-16”},
{“外围设备”,“123错误”}
};
var wasItThere=response.TryGetValue(名称(搜索持续时间),输出var值);
WriteLine(wasItThere&(value==searchDuration));
TryGetValue
优于
ContainsKey
,因为它在检查键是否存在的同时获取值

nameof
用于将变量名转换为其字符串表示形式


我明确地没有使用
pair.Value
,因为您原始问题中的代码强烈暗示您正在迭代
字典。这不是一个好主意(性能方面)。

我建议采用以下方法:

string searchDuration = "200";

var response = new Dictionary<string, string>()
{
    {"searchDuration","200"},
    {"minRssi", "-70"},
    {"optionalFilter","NO_FILTERS_ACTIVE_SCANNING"},
    {"txPowerLevel","-16"},
    {"peripheralId","123wrong"}
};

var wasItThere = response.TryGetValue(nameof(searchDuration), out var value);
Console.WriteLine(wasItThere && (value == searchDuration));
string searchDuration=“200”;
var response=newdictionary()
{
{“搜索持续时间”,“200”},
{“minRssi”,“-70”},
{“optionalFilter”、“NO_FILTERS\u ACTIVE_SCANNING”},
{“txPowerLevel”,“-16”},
{“外围设备”,“123错误”}
};
var wasItThere=response.TryGetValue(名称(搜索持续时间),输出var值);
WriteLine(wasItThere&(value==searchDuration));
TryGetValue
优于
ContainsKey
,因为它在检查键是否存在的同时获取值

nameof
用于将变量名转换为其字符串表示形式


我明确地没有使用
pair.Value
,因为您原始问题中的代码强烈暗示您正在迭代
字典。这不是一个好主意(性能方面)。

如果要比较的变量都是对象的一部分,则可以通过反射检查该对象,并将对象内部的内容与字典中的内容进行比较。以下是如何:

using System;
using System.Collections.Generic;
using System.Linq;

public class Program
{
    public static void Main()
    {
        var obj = new { searchDuration = "200", txPowerLevel = "100", other = "123"};

        var stringProperties = obj
            .GetType()
            .GetProperties()
            .Where(pi => pi.PropertyType == typeof(string) && pi.GetGetMethod() != null)
            .Select(pi => new
            {
                Name = pi.Name,
                Value = pi.GetGetMethod().Invoke(obj, null)}
            )
            .ToList();
        
        var response = new Dictionary<string, string>()
        {
            {"searchDuration","200"},
            {"minRssi", "-70"},
            {"optionalFilter","NO_FILTERS_ACTIVE_SCANNING"},
            {"txPowerLevel","200"},
            {"peripheralId","123wrong"}
        };

        foreach (var item in stringProperties)
        {
            string v;
            response.TryGetValue(item.Name, out v);         
            Console.WriteLine(item.Name + ": obj value=" + item.Value + ", response value=" + (v ?? "--N/A--"));
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
公共课程
{
公共静态void Main()
{
var obj=new{searchDuration=“200”,txPowerLevel=“100”,other=“123”};
var stringProperties=obj
.GetType()
.GetProperties()
.Where(pi=>pi.PropertyType==typeof(string)和&pi.getMethod()!=null)
.选择(pi=>new
{
Name=pi.Name,
Value=pi.getMethod().Invoke(obj,null)}
)
.ToList();
var response=newdictionary()
{
{“搜索持续时间”,“200”},
{“minRssi”,“-70”},
{“optionalFilter”、“NO_FILTERS\u ACTIVE_SCANNING”},
{“txPowerLevel”,“200”},
{“外围设备”,“123错误”}
};
foreach(stringProperties中的变量项)
{
字符串v;
响应.TryGetValue(item.Name,out v);
Console.WriteLine(item.Name+”:obj value=“+item.value+”,response value=“+(v???--N/A-->”);
}
}
}
工作小提琴:


如果项目以局部变量的形式出现,则可能也可以这样做(例如,请参见),但我建议将其放在对象中,以便将要检查的值与方法需要和使用的其他变量分开。

如果要比较的变量都是对象的一部分,然后,您可以通过反射检查该对象,并将在该对象内部找到的内容与字典中存在的内容进行比较。以下是如何:

using System;
using System.Collections.Generic;
using System.Linq;

public class Program
{
    public static void Main()
    {
        var obj = new { searchDuration = "200", txPowerLevel = "100", other = "123"};

        var stringProperties = obj
            .GetType()
            .GetProperties()
            .Where(pi => pi.PropertyType == typeof(string) && pi.GetGetMethod() != null)
            .Select(pi => new
            {
                Name = pi.Name,
                Value = pi.GetGetMethod().Invoke(obj, null)}
            )
            .ToList();
        
        var response = new Dictionary<string, string>()
        {
            {"searchDuration","200"},
            {"minRssi", "-70"},
            {"optionalFilter","NO_FILTERS_ACTIVE_SCANNING"},
            {"txPowerLevel","200"},
            {"peripheralId","123wrong"}
        };

        foreach (var item in stringProperties)
        {
            string v;
            response.TryGetValue(item.Name, out v);         
            Console.WriteLine(item.Name + ": obj value=" + item.Value + ", response value=" + (v ?? "--N/A--"));
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
公共课程
{
公共静态void Main()
{
var obj=new{searchDuration=“200”,txPowerLevel=“100”,other=“123”};
var stringProperties=obj
.GetType()
.GetProperties()
.Where(pi=>pi.PropertyType==typeof(string)和&pi.getMethod()!=null)
.选择(pi=>new
{
Name=pi.Name,
Value=pi.getMethod().Invoke(obj,null)}
)
.ToList();
var response=newdictionary()
{
{“搜索持续时间”,“200”},
{“minRssi”,“-70”},
{“optionalFilter”、“NO_FILTERS\u ACTIVE_SCANNING”},
{“txPowerLevel”,“200”},
{“外围设备”,“123错误”}
};
foreach(stringProperties中的变量项)
{
字符串v;
响应.TryGetValue(item.Name,out v);
Console.WriteLine(item.Name+”:obj value=“+item.value+”,response value=“+(v???--N/A-->”);
}
}
}
工作小提琴:


如果项目以局部变量的形式出现,那么也可以这样做(例如,请参见),但我建议将其放入对象中,以使要检查的值与方法需要和使用的其他变量分开。

有简单的解决方案,问题是为什么要这样做?我有来自Http响应的键值对