所以,我有这个代码
Process[] processesManager = Process.GetProcesses();
List<ProcessInfo> temporaryProcessesList = new List<ProcessInfo>();
for (int i = 0; i < processesManager.Length; ++i)
{
temporaryProcessesList.Add(new ProcessInfo(processesManager[i].ProcessName, processesManager[i].Id, TimeSpan.Zero, "Group"));
}
processesList = temporaryProcessesList.GroupBy(d => new {d.Name}).Select(d => d.First()).ToList();
此代码用于获取当前进程。然后我将这些过程添加到temporaryProcessesList
。而不是简单的字符串&#34; Group&#34;我想根据进程名称设置属性。例如,如果进程名称为leagueoflegends.exe,那么我想将组设置为&#34; Games&#34;,如果它的devenv.exe我想将组设置为&#34;软件开发&#34;。
我的问题是,如何以最简单/最好的方式做到这一点?我正在考虑将字符串与字符串和枚举一起使用。并将ProcessName
与字符串进行比较。但也许有更好的方法来做到这一点。
ProcessInfo
是一个简单的类,包含4个属性和构造函数。
public class ProcessInfo
{
private string Name { get; set; }
private int Id { get; set; }
private TimeSpan Time { get; set; }
private string Group { get; set; }
public ProcessInfo(string name, int id, TimeSpan time, string group)
{
Name = name;
Id = id;
Time = time;
Group = group;
}
}
答案 0 :(得分:3)
使用字典是实现此目的的最佳方式:
var dictionary = new Dictionary<string, string>();
dictionary.Add("a.exe", "aGroup");
dictionary.Add("b.exe", "bGroup");
string val;
if (dictionary.TryGetValue(processName, out val))
processInfo.Group = val;
else
processInfo.Group = "Undefined Group";
答案 1 :(得分:1)
也许这就是你要找的东西:
# file: pyenv\lib\site-packages\cryptography\hazmat\backends\__init__.py
def _available_backends():
global _available_backends_list
if _available_backends_list is None:
_available_backends_list = [
ep.resolve()
for ep in pkg_resources.iter_entry_points(
"cryptography.backends"
)
]
# patch starts here
if _available_backends_list is None or len(_available_backends_list) == 0:
# backend 1
try:
from cryptography.hazmat.backends.commoncrypto.backend import backend as be_cc
except ImportError:
be_cc = None
# backend 2
try:
from cryptography.hazmat.backends.openssl.backend import backend as be_ossl
except ImportError:
be_ossl = None
# add any backends of your own
_available_backends_list = [
be for be in (be_cc, be_ossl) if be is not None
]
# patch ends here
return _available_backends_list
这种设计并不完美,但希望你能看到这个想法。您还可以将public class ProcessInfo
{
private string _name;
private string Name
{
get { return _name; }
set
{
_name = value;
UpdateGroupName();
}
}
private int Id { get; set; }
private TimeSpan Time { get; set; }
private string Group { get; set; }
private void UpdateGroupName()
{
Group = ProcessNames::GetGroupFromProcessName(Name);
}
public ProcessInfo(string name, int id, TimeSpan time)
{
Name = name;
Id = id;
Time = time;
}
}
internal static class ProcessNames
{
private static Dictionary<string, string> _names;
public static string GetGroupNameFromProcessName(string name)
{
// Make sure to add locking if there is a possibility of using
// this from multiple threads.
if(_names == null)
{
// Load dictionary from JSON file
}
// Return the value from the Dictionary here, if it exists.
}
}
名称的更新移动到构造函数,但如果在构造之后设置属性,则不会更改Group
名称。
此外,您可以使用Group
和/或依赖注入来清理界面。 https://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged(v=vs.110).aspx