如何处理列表中的特定元素
How to address specific element in list
我想创建 "list of list of list"。应该是:
组(有成员列表)
成员(有名称和属性列表)
属性(有名称和值)
我想要的是可以将 属性 添加到已定义组内的成员(由其名称指定)中。像这样:
membersgroup.AddNewMember(memberXYZ);
...
membersgroup.memberXYZ.AddProperty(nameXYZ, valueXYZ).
我无法使用列表实现此目的...我发现 class 可散列,但我不确定这是否可用...并且不能使其也有效...
感谢任何建议:)
好吧,我建议您创建一个自定义 class 而不是您的方法。但否则你可以使用字典。
var properties = new Dictionary<string, string>();
properties.Add("Prop1", "Value");
var members = new Dictionary<string, Dictionary<string, string>>();
members.Add("Member1", properties);
var group = new Dictionary<string, Dictionary<string, Dictionary<string, string>>>();
group.Add("GroupName", members);
我会使用索引器。
这是部分实现:
class Group
{
private List<Member> _members;
public string this
{
get
{
return _members.Find(m => m.Name == value);
}
// You can also implement set here if you want...
}
}
class Member
{
private List<Property> _properties;
public string Name {get;set;}
public string this
{
get
{
return _properties.Find(m => m.Name == value);
}
}
}
class Property
{
public string Name {get;set;}
public string Value {get;set;}
}
以及用法:
var g = new Group();
g[memberName][propertyName].Value = someValue;
注意:此实现是部分的!它仍然需要构造函数逻辑和您可能需要的任何其他逻辑。
public class Group
{
public Group()
{
Members = new List<Member>();
}
public IEnumerable<Member> Members { get; set; }
}
public class Member
{
public Member()
{
Properties = new Dictionary<string, string>();
}
public string Name { get; set; }
IDictionary<string, string> Properties { get; set; }
}
字典可以有一个键和一个值,键应该是唯一的。
如果您想在名称和值旁边添加其他内容,您还可以创建 class 属性
可能最好的解决方案是使用 C# class Dictionary - as suggested by , or a custom class - as suggested by , or some mix of the two - as suggested by .
但是,为了使用类似于问题中要求的语法...
membersgroup.AddNewMember(memberXYZ);
...
membersgroup.memberXYZ.AddProperty(nameXYZ, valueXYZ).
你可以滥用 ExpandoObject and Action,然后像这样做一些很棒的:
dynamic membersgroup = new ExpandoObject();
var getNewMemberObject = new Func<dynamic>(() =>
{
dynamic memberObject = new ExpandoObject();
var addPropertyAction = new Action<string, string>((propertyName, propertyValue) =>
{
((IDictionary<string, object>)memberObject).Add(propertyName, propertyValue);
});
memberObject.AddProperty = addPropertyAction;
return memberObject;
});
var addNewMemberAction = new Action<string>((memberName) =>
{
((IDictionary<string, object>)membersgroup).Add(memberName, getNewMemberObject());
});
membersgroup.AddNewMember = addNewMemberAction;
string memberXYZ = nameof(memberXYZ);
string nameXYZ = nameof(nameXYZ);
string valueXYZ = nameof(valueXYZ);
// look we did it!
membersgroup.AddNewMember(memberXYZ);
membersgroup.memberXYZ.AddProperty(nameXYZ, valueXYZ);
// and it actually works
var actualValue = membersgroup.memberXYZ.nameXYZ;
Console.WriteLine(actualValue); // "valueXYZ"
(当然是为了科学)[=25=]
我想创建 "list of list of list"。应该是:
组(有成员列表)
成员(有名称和属性列表)
属性(有名称和值)
我想要的是可以将 属性 添加到已定义组内的成员(由其名称指定)中。像这样:
membersgroup.AddNewMember(memberXYZ);
...
membersgroup.memberXYZ.AddProperty(nameXYZ, valueXYZ).
我无法使用列表实现此目的...我发现 class 可散列,但我不确定这是否可用...并且不能使其也有效...
感谢任何建议:)
好吧,我建议您创建一个自定义 class 而不是您的方法。但否则你可以使用字典。
var properties = new Dictionary<string, string>();
properties.Add("Prop1", "Value");
var members = new Dictionary<string, Dictionary<string, string>>();
members.Add("Member1", properties);
var group = new Dictionary<string, Dictionary<string, Dictionary<string, string>>>();
group.Add("GroupName", members);
我会使用索引器。
这是部分实现:
class Group
{
private List<Member> _members;
public string this
{
get
{
return _members.Find(m => m.Name == value);
}
// You can also implement set here if you want...
}
}
class Member
{
private List<Property> _properties;
public string Name {get;set;}
public string this
{
get
{
return _properties.Find(m => m.Name == value);
}
}
}
class Property
{
public string Name {get;set;}
public string Value {get;set;}
}
以及用法:
var g = new Group();
g[memberName][propertyName].Value = someValue;
注意:此实现是部分的!它仍然需要构造函数逻辑和您可能需要的任何其他逻辑。
public class Group
{
public Group()
{
Members = new List<Member>();
}
public IEnumerable<Member> Members { get; set; }
}
public class Member
{
public Member()
{
Properties = new Dictionary<string, string>();
}
public string Name { get; set; }
IDictionary<string, string> Properties { get; set; }
}
字典可以有一个键和一个值,键应该是唯一的。 如果您想在名称和值旁边添加其他内容,您还可以创建 class 属性
可能最好的解决方案是使用 C# class Dictionary - as suggested by
但是,为了使用类似于问题中要求的语法...
membersgroup.AddNewMember(memberXYZ);
...
membersgroup.memberXYZ.AddProperty(nameXYZ, valueXYZ).
你可以滥用 ExpandoObject and Action,然后像这样做一些很棒的:
dynamic membersgroup = new ExpandoObject();
var getNewMemberObject = new Func<dynamic>(() =>
{
dynamic memberObject = new ExpandoObject();
var addPropertyAction = new Action<string, string>((propertyName, propertyValue) =>
{
((IDictionary<string, object>)memberObject).Add(propertyName, propertyValue);
});
memberObject.AddProperty = addPropertyAction;
return memberObject;
});
var addNewMemberAction = new Action<string>((memberName) =>
{
((IDictionary<string, object>)membersgroup).Add(memberName, getNewMemberObject());
});
membersgroup.AddNewMember = addNewMemberAction;
string memberXYZ = nameof(memberXYZ);
string nameXYZ = nameof(nameXYZ);
string valueXYZ = nameof(valueXYZ);
// look we did it!
membersgroup.AddNewMember(memberXYZ);
membersgroup.memberXYZ.AddProperty(nameXYZ, valueXYZ);
// and it actually works
var actualValue = membersgroup.memberXYZ.nameXYZ;
Console.WriteLine(actualValue); // "valueXYZ"
(当然是为了科学)[=25=]