[英]cast KeyValuePair<Guid, object> to KeyValuePair<string, object>
What would be the easiest way to cast: 最简单的投射方法是:
IList<KeyValuePair<Guid, object>>
to 至
IList<KeyValuePair<string, object>>
without loops etc. 没有循环等
You can't cast between KeyValuePair<Guid, object>
and KeyValuePair<string, object>
, they are incompatible types (the same goes for IList<>
s of them, unless you're using a class that implements both). 您不能在
KeyValuePair<Guid, object>
和KeyValuePair<string, object>
之间进行KeyValuePair<Guid, object>
,它们是不兼容的类型(它们的IList<>
都是一样的,除非您使用同时实现两者的类)。 You could create a new KeyValuePair<string, object>
with similar values by using ToString()
to turn the Guid
into a string
: 您可以使用
ToString()
将Guid
转换为string
以创建具有相似值的新KeyValuePair<string, object>
:
var newKvp = new KeyValuePair<string, object>(old.Key.ToString(), old.Value);
To do this to an IList<KeyValuePair<Guid, object>>
, you'll have to do a loop of some sort. 要对
IList<KeyValuePair<Guid, object>>
,则必须进行某种循环。 I'd suggest using LINQ, for readability: 我建议使用LINQ,以提高可读性:
IList<KeyValuePair<string, object>> newList = oldList
.Select(x => new KeyValuePair<string, object>(x.Key.ToString(), x.Value))
.ToList();
You can make a wrapper IList
class that will do it without loops - but you have to provide conversion (derived-parent, parent-derived) yourself. 您可以创建一个包装器
IList
类,该类无需循环即可完成-但是您必须自己提供转换(派生父级,派生父级)。
Usage: 用法:
var originalList = new List<KeyValuePair<Guid, object>>();
originalList.Add(new KeyValuePair<Guid, object>(Guid.Empty, "blabla"));
originalList.Add(new KeyValuePair<Guid, object>(Guid.NewGuid(), "foobar"));
var list = new ConvertibleList<KeyValuePair<string, object>, KeyValuePair<Guid, object>>(originalList,
(k) => new KeyValuePair<string, object>(k.Key.ToString("N"), k.Value),
(k) => new KeyValuePair<Guid, object>(new Guid(k.Key), k.Value));
Wrapper class: 包装类:
public class ConvertibleList<Derived,Parent> : IList<Derived> {
private IList<Parent> m_List;
private Func<Parent, Derived> m_ParentToDerived;
private Func<Derived, Parent> m_DerivedToParent;
private class Enumerator : IEnumerator<Derived> {
private IEnumerator<Parent> m_Enumerator;
private Func<Parent, Derived> m_ParentToDerived;
public Enumerator(IEnumerator<Parent> enumerator, Func<Parent, Derived> parentToDerived) {
m_Enumerator = enumerator;
m_ParentToDerived = parentToDerived;
}
public Derived Current {
get { return m_ParentToDerived(m_Enumerator.Current); }
}
object IEnumerator.Current {
get { return m_ParentToDerived(m_Enumerator.Current); }
}
public bool MoveNext() {
return m_Enumerator.MoveNext();
}
public void Reset() {
m_Enumerator.Reset();
}
public void Dispose() {
m_Enumerator.Dispose();
}
}
private class Enumerable : IEnumerable<Derived> {
private IEnumerable<Parent> m_Parent;
private Func<Parent, Derived> m_ParentToDerived;
public Enumerable(IEnumerable<Parent> parent, Func<Parent, Derived> parentToDerived) {
m_Parent = parent;
m_ParentToDerived = parentToDerived;
}
public IEnumerator<Derived> GetEnumerator() {
return new Enumerator(m_Parent.GetEnumerator(), m_ParentToDerived);
}
IEnumerator IEnumerable.GetEnumerator() {
return new Enumerator(m_Parent.GetEnumerator(), m_ParentToDerived);
}
}
public ConvertibleList(IList<Parent> list, Func<Parent, Derived> parentToDerived, Func<Derived, Parent> derivedToParent) {
if (list == null) {
throw new ArgumentNullException("list");
}
m_List = list;
m_ParentToDerived = parentToDerived;
m_DerivedToParent = derivedToParent;
}
public int IndexOf(Derived item) {
return m_List.IndexOf(m_DerivedToParent(item));
}
public void Insert(int index, Derived item) {
m_List.Insert(index, m_DerivedToParent(item));
}
public void RemoveAt(int index) {
m_List.RemoveAt(index);
}
public Derived this[int index] {
get { return m_ParentToDerived(m_List[index]); }
set { m_List[index] = m_DerivedToParent(value); }
}
public void Add(Derived item) {
m_List.Add(m_DerivedToParent(item));
}
public void Clear() {
m_List.Clear();
}
public bool Contains(Derived item) {
return m_List.Contains(m_DerivedToParent(item));
}
public void CopyTo(Derived[] array, int arrayIndex) {
var parentArray = new Parent[array.Length];
for (var i = 0; i < array.Length; i++) {
parentArray[i] = m_DerivedToParent(array[i]);
}
m_List.CopyTo(parentArray, arrayIndex);
}
public int Count {
get { return m_List.Count; }
}
public bool IsReadOnly {
get { return m_List.IsReadOnly; }
}
public bool Remove(Derived item) {
return m_List.Remove(m_DerivedToParent(item));
}
public IEnumerator<Derived> GetEnumerator() {
return new Enumerator(m_List.GetEnumerator(), m_ParentToDerived);
}
IEnumerator IEnumerable.GetEnumerator() {
return new Enumerator(m_List.GetEnumerator(), m_ParentToDerived);
}
}
如果您有KeyValuePair<Guid, object>
k,则可以执行以下操作:
new KeyValuePair<string, object>(k.Key.ToString(), k.Value);
When you say: 当你说:
without loops etc.
没有循环等
Does this imply you have multiple KeyValuePairs
? 这是否意味着您有多个
KeyValuePairs
? So say you have IEnumerable<KeyValuePair<Guid, Object>>
and want IEnumerable<KeyValuePair<string, Object>>
... You will HAVE to do some form of looping to convert each value: 假设您有
IEnumerable<KeyValuePair<Guid, Object>>
并想要IEnumerable<KeyValuePair<string, Object>>
...您将必须执行某种形式的循环以转换每个值:
So you could very easily do it with a LINQ statement: 因此,您可以使用LINQ语句轻松完成此操作:
IEnumerable<KeyValuePair<Guid, Object>> x = ...;
var newPairs = x.Select(kvp=> new KeyValuePair<string, object>(kvp.Key.ToString(), kvp.Value));
This is at least pretty clean... 这至少很干净...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.