我已经围绕第 3 方库编写了一个包装类,该类需要通过调用 Config 方法并传递格式为“Property=Value”的字符串来设置属性
。我想在一次调用中传递所有属性并迭代处理它们。
我考虑了以下内容:
- 创建一个属性/值类,然后创建一个列表
对象
- 构建由多个“Property=Value”分隔的字符串的
使用令牌(可能是“|”)
- 使用哈希表
所有这些都可以工作(我正在考虑使用选项 1),但是有更好的方法吗?
有关我的查询的更多详细信息:
完成的类将包含在库中,以便在其他应用程序中重复使用。虽然我目前不认为线程是一个问题(我们的应用程序往往只有一个 UI 线程和一个工作线程),但它将来可能会成为一个问题。
垃圾收集不会成为问题。
目前,访问数据源的任意索引不是问题。
优化目前不是问题,但明确定义键/值对很重要。
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"
I'd like to pass all the properties in a single call and process them iteratively.
I've considered the following:
- creating a property/value class and then creating a List of these
objects
- building a string of multiple "Property=Value" separating them
with a token (maybe "|")
- Using a hash table
All of these would work (and I'm thinking of using option 1) but is there a better way?
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.
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.
发布评论
评论(4)
正如您已经指出的,任何建议的解决方案都将完成您所描述的任务。这意味着选择特定方法的唯一合理方法是定义您的需求:
List>
。字典
是更好的选择。如果没有完全定义功能需要做什么,您就无法就如何实现功能做出明智的决定,并且由于您还没有这样做,所以任何答案这里给出的内容必然是不完整的。
鉴于您的说明,我个人建议如下:
根据 MSDN 指南,默认情况下避免使 Config() 方法成为线程安全的:
<块引用>
默认情况下,类库不应该是线程安全的。添加锁来创建线程安全代码会降低性能,增加锁争用,并可能导致发生死锁错误。
如果线程安全稍后变得重要,则使其由调用者负责。
如果您没有特殊的性能要求,请坚持使用字典,以便轻松定义和读取键/值对。
为了简单起见,并避免在连接时生成大量不必要的字符串,只需直接传递字典并迭代它。
考虑以下示例:
以及 Config 的实现:
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. Otherwise, there's no reason to incur the additional overhead and complexity of using a concurrent data structure.List<KeyValuePair<String, String>>
instead.Dictionary
is a better choice.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:
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:
And the implementation of Config:
您可以使用
Dictionary
,然后项目的类型为KeyValuePair
(这对应于您的第一个想法)然后,您可以使用
myDict.Select(kvp=>string.Format("{0}={1}",kvp.Key,kvp.Value))
获取字符串列表,其中需要格式化You could use
Dictionary<string,string>
, the items are then of typeKeyValuePair<string,string>
(this correpsonds to your first idea)You can then use
myDict.Select(kvp=>string.Format("{0}={1}",kvp.Key,kvp.Value))
to get a list of strings with the needed formatting例如使用
ConcurrentDictionary
- 它是线程安全的并且非常快,因为大多数操作都是无锁实现的......Use for example a
ConcurrentDictionary<string,string>
- it is thread-safe and really fast since most operations are implemented lock-free...您可以创建一个辅助类,它使用反射将任何类转换为 Property=Value 集合。
您需要添加额外的逻辑来处理枚举、索引属性等。
You could make a helper class that uses reflection to turn any class into a Property=Value collection
You would need to add extra logic to handle enumerations, indexed properties, etc.