[英]What is the best way to implement a property=value collection
I've written a wrapper class around a 3rd party library that requires properties to be set by calling a Config method and passing a string formatted as " Property=Value " 我已经围绕第三方库编写了一个包装类,它需要通过调用Config方法并传递格式为“ Property = Value ”的字符串来设置属性。
I'd like to pass all the properties in a single call and process them iteratively. 我想在一次调用中传递所有属性并迭代地处理它们。
I've considered the following: 我考虑过以下几点:
All of these would work (and I'm thinking of using option 1) but is there a better way? 所有这些都可行(我正在考虑使用选项1)但是有更好的方法吗?
A bit more detail about my query: 关于我的查询的更多细节:
The finished class will be included in a library for re-use in other applications. 完成的类将包含在库中,以便在其他应用程序中重用。 Whilst I don't currently see threading as a problem at the moment (our apps tend to just have a UI thread and a worker thread) it could become an issue in the future.
虽然我目前没有将线程视为一个问题(我们的应用程序往往只有一个UI线程和一个工作线程),但它可能会成为未来的问题。
Garbage collection will not be an issue. 垃圾收集不会成为问题。
Access to arbitrary indices of the data source is not currently an issue. 目前,访问数据源的任意索引不是问题。
Optimization is not currently an issue but clearly define the key/value pairs is important. 优化目前不是问题,但明确定义键/值对很重要。
您可以使用Dictionary<string,string>
,这些项目的类型为KeyValuePair<string,string>
(这与您的第一个想法相对应)您可以使用myDict.Select(kvp=>string.Format("{0}={1}",kvp.Key,kvp.Value))
获取具有所需格式的字符串列表
As you've already pointed out, any of the proposed solutions will accomplish the task as you've described it. 正如您已经指出的那样,任何提议的解决方案都将完成您所描述的任务。 What this means is that the only rational way to choose a particular method is to define your requirements:
这意味着选择特定方法的唯一合理方法是定义您的要求:
ConcurrentDictionary
, as Yahia suggested, makes sense. ConcurrentDictionary
是有道理的。 Otherwise, there's no reason to incur the additional overhead and complexity of using a concurrent data structure. List<KeyValuePair<String, String>>
instead. List<KeyValuePair<String, String>>
。 Dictionary
is a better choice. Dictionary
是一个更好的选择。 You can't make an informed decision as to how to implement a feature without completely defining what the feature needs to do , and since you haven't done that, any answer given here will necessarily be incomplete. 如果没有完全定义功能需要 执行的功能,您无法做出如何实现功能的明智决定,并且由于您没有这样做,因此此处给出的任何答案都必然是不完整的。
Given your clarifications, I would personally suggest the following: 鉴于您的澄清,我个人会建议如下:
Avoid making your Config() method thread-safe by default, as per the MSDN guidelines: 根据MSDN指南,默认情况下避免使Config()方法成为线程安全的:
By default, class libraries should not be thread safe.
默认情况下,类库不应该是线程安全的。 Adding locks to create thread-safe code decreases performance, increases lock contention, and creates the possibility for deadlock bugs to occur.
添加锁以创建线程安全的代码会降低性能,增加锁争用,并且可能会发生死锁错误。
If thread safety becomes important later, make it the caller's responsibility. 如果线程安全性在以后变得重要,请将其作为调用者的责任。
Given that you don't have special performance requirements, stick with a dictionary to allow key/value pairs to be easily defined and read. 鉴于您没有特殊的性能要求,请坚持使用字典以便轻松定义和读取键/值对。
For simplicity's sake, and to avoid generating lots of unnecessary strings doing concatenations, just pass the dictionary in directly and iterate over it. 为简单起见,为避免生成大量不必要的字符串进行连接,只需直接传递字典并迭代它。
Consider the following example: 请考虑以下示例:
var configData = new Dictionary<String, String>
configData["key1"] = "value1";
configData["key2"] = "value2";
myLibraryObject.Config(configData);
And the implementation of Config: 并且Config的实现:
public void Config(Dictionary<String, String> values)
{
foreach(var kvp in values)
{
var configString = String.Format("{0}={1}", kvp.Key, kvp.Value);
// do whatever
}
}
例如,使用ConcurrentDictionary<string,string>
- 它是线程安全且非常快,因为大多数操作都是无锁实现的......
You could make a helper class that uses reflection to turn any class into a Property=Value collection 您可以创建一个使用反射的帮助程序类,将任何类转换为Property = Value集合
public static class PropertyValueHelper
{
public static IEnumerable<string> GetPropertyValues(object source)
{
Type t = source.GetType();
foreach (var property in t.GetProperties())
{
object value = property.GetValue(source, null);
if (value != null)
{
yield return property.Name + "=" + value.ToString();
}
else
{
yield return property.Name + "=";
}
}
}
}
You would need to add extra logic to handle enumerations, indexed properties, etc. 您需要添加额外的逻辑来处理枚举,索引属性等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.