C# 根据其他属性设置属性的方法
所以,我有这个代码C# 根据其他属性设置属性的方法,c#,.net,c#-4.0,c#-3.0,c#-2.0,C#,.net,C# 4.0,C# 3.0,C# 2.0,所以,我有这个代码 Process[] processesManager = Process.GetProcesses(); List<ProcessInfo> temporaryProcessesList = new List<ProcessInfo>(); for (int i = 0; i < processesManager.Length; ++i) { temporar
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();
使用字典是实现此目的的最佳方法:
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";
var dictionary=newdictionary();
添加(“a.exe”、“aGroup”);
添加(“b.exe”、“bGroup”);
字符串val;
if(dictionary.TryGetValue(processName,out val))
processInfo.Group=val;
其他的
processInfo.Group=“未定义的组”;
也许这就是你要找的:
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.
}
}
公共类ProcessInfo
{
私有字符串\u名称;
私有字符串名
{
获取{return\u name;}
设置
{
_名称=值;
UpdateGroupName();
}
}
私有int Id{get;set;}
私有时间跨度时间{get;set;}
私有字符串组{get;set;}
私有void UpdateGroupName()
{
Group=ProcessName::GetGroupFromProcessName(名称);
}
公共ProcessInfo(字符串名称、int id、TimeSpan时间)
{
名称=名称;
Id=Id;
时间=时间;
}
}
内部静态类进程名
{
私有静态字典_名称;
公共静态字符串GetGroupNameFromProcessName(字符串名称)
{
//如果有可能使用,请确保添加锁定
//这可以从多个线程执行。
如果(_name==null)
{
//从JSON文件加载字典
}
//返回字典中的值(如果存在)。
}
}
这个设计并不完美,但希望你能看到这个想法。您还可以将组
名称的更新移动到构造函数,但是如果在构造之后设置属性,则不会更改组
名称
此外,您还可以使用
INotifyPropertyChanged
和/或依赖项注入来清理接口 谢谢你的回答。但也许有人会知道其他的方法,它更像是一种写作的方式,而不是使用字典,因为我需要为每个过程编写案例。是的,这是另一种解决方案。没错,但是您可以更新UpdateGroupName
方法,从文本文件、XML文件等中读取进程名及其对应组名的列表。这样,您就可以更新组名,而无需重新生成.Hymm。。我想用字典做这件事。我的意思是从json文件读入字典,我认为这是更好的方法。你不觉得吗?我会这么做的。创建一个加载并缓存JSON文件的类,然后使用该类根据进程名设置组名。我会更新答案中的代码,给你一个想法。
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.
}
}