遍历结构中存在的 属性 中的值
Looping over the value inside property which is present inside the struct
我创建了一个具有一些属性的结构,例如
public struct DeviceDetailModel
{
public static readonly DeviceDetailModel DT851P = new DeviceDetailModel("851P","v1","v2");
public static readonly DeviceDetailModel DT852P = new DeviceDetailModel("852P","v3","v4");
public static readonly DeviceDetailModel DT83P = new DeviceDetailModel("853P","v5","v6");
public static readonly DeviceDetailModel DT854P = new DeviceDetailModel("854P");
public string DeviceName { get; private set; }
public string Value1 { get; private set; }
public string Value2 { get; private set; }
private DeviceDetailModel(string deviceName,string value1,string value2)
{
DeviceName = deviceName;
Value1 = value1;
Value2 = value2;
}
}
现在,如果我想获得单个项目的详细信息,我只需要做 DeviceDetailModel.DT854P
但问题是我会在 运行time 上获得一个值,我必须使用它来确定我必须 return
哪个结构 属性
例如=我的运行时间值是853P
我想循环遍历我的结构以确定 DeviceName
中的何处与该值 853P
匹配以及哪个应该 return DeviceDetailModel.DT83P
我能够遍历结构的属性,但无法获取值
编辑:根据我的 运行 时间值,我需要迭代 DeviceName
的值,如果该值匹配,它应该 return 关联 属性
这是一个相当简单的选项:
public struct DeviceDetailModel
{
private static readonly Dictionary<string, DeviceDetailModel> models = new Dictionary<string, DeviceDetailModel>
{
{"851P", new DeviceDetailModel("851P")},
{"852P", new DeviceDetailModel("852P")},
{"853P", new DeviceDetailModel("853P")},
{"854P", new DeviceDetailModel("854P")},
};
public static DeviceDetailModel DT851P get => models["851P"];
public static DeviceDetailModel DT852P get => models["852P"];
public static DeviceDetailModel DT83P get => models["853P"];
public static DeviceDetailModel DT854P get => models["854P"];
private DeviceDetailModel(string deviceName)
{
DeviceName = deviceName;
}
public string DeviceName {get;private set;}
public DeviceDetailModel? FindByDeviceName(string deviceName)
{
return models.TryGetValue(deviceName, out var value) ? value : (DeviceDetailModel)null;
}
}
请注意,FindByDeviceName
的 return 值是 Nullable<DeviceDetailModel>
,因此如果您要查找未找到的字符串,您不会得到异常,但 null
.
我创建了一个具有一些属性的结构,例如
public struct DeviceDetailModel
{
public static readonly DeviceDetailModel DT851P = new DeviceDetailModel("851P","v1","v2");
public static readonly DeviceDetailModel DT852P = new DeviceDetailModel("852P","v3","v4");
public static readonly DeviceDetailModel DT83P = new DeviceDetailModel("853P","v5","v6");
public static readonly DeviceDetailModel DT854P = new DeviceDetailModel("854P");
public string DeviceName { get; private set; }
public string Value1 { get; private set; }
public string Value2 { get; private set; }
private DeviceDetailModel(string deviceName,string value1,string value2)
{
DeviceName = deviceName;
Value1 = value1;
Value2 = value2;
}
}
现在,如果我想获得单个项目的详细信息,我只需要做 DeviceDetailModel.DT854P
但问题是我会在 运行time 上获得一个值,我必须使用它来确定我必须 return
哪个结构 属性例如=我的运行时间值是853P
我想循环遍历我的结构以确定 DeviceName
中的何处与该值 853P
匹配以及哪个应该 return DeviceDetailModel.DT83P
我能够遍历结构的属性,但无法获取值
编辑:根据我的 运行 时间值,我需要迭代 DeviceName
的值,如果该值匹配,它应该 return 关联 属性
这是一个相当简单的选项:
public struct DeviceDetailModel
{
private static readonly Dictionary<string, DeviceDetailModel> models = new Dictionary<string, DeviceDetailModel>
{
{"851P", new DeviceDetailModel("851P")},
{"852P", new DeviceDetailModel("852P")},
{"853P", new DeviceDetailModel("853P")},
{"854P", new DeviceDetailModel("854P")},
};
public static DeviceDetailModel DT851P get => models["851P"];
public static DeviceDetailModel DT852P get => models["852P"];
public static DeviceDetailModel DT83P get => models["853P"];
public static DeviceDetailModel DT854P get => models["854P"];
private DeviceDetailModel(string deviceName)
{
DeviceName = deviceName;
}
public string DeviceName {get;private set;}
public DeviceDetailModel? FindByDeviceName(string deviceName)
{
return models.TryGetValue(deviceName, out var value) ? value : (DeviceDetailModel)null;
}
}
请注意,FindByDeviceName
的 return 值是 Nullable<DeviceDetailModel>
,因此如果您要查找未找到的字符串,您不会得到异常,但 null
.