cast KeyValuePair<Guid, object> to KeyValuePair<str

本文关键字:lt KeyValuePair to str gt Guid cast object | 更新日期: 2023-09-27 18:10:25

最简单的转换方式是:

IList<KeyValuePair<Guid, object>>

IList<KeyValuePair<string, object>>

不带循环等

cast KeyValuePair<Guid, object> to KeyValuePair<str

您不能在KeyValuePair<Guid, object>KeyValuePair<string, object>之间进行强制转换,它们是不兼容的类型(IList<>的类型也是如此,除非您使用实现两者的类)。您可以通过使用ToString()Guid转换为string来创建具有类似值的新KeyValuePair<string, object>:

var newKvp = new KeyValuePair<string, object>(old.Key.ToString(), old.Value);

要对IList<KeyValuePair<Guid, object>>执行此操作,您必须执行某种循环。为了可读性,我建议使用LINQ:

IList<KeyValuePair<string, object>> newList = oldList
  .Select(x => new KeyValuePair<string, object>(x.Key.ToString(), x.Value))
  .ToList();

您可以创建一个没有循环的包装器IList类,但是您必须自己提供转换(派生-父,父-派生)。

用法:

  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));

包装类:

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);

当你说:

不带循环等

这是否意味着您有多个KeyValuePairs ?假设你有IEnumerable<KeyValuePair<Guid, Object>>,想要IEnumerable<KeyValuePair<string, Object>>…您必须执行某种形式的循环来转换每个值:

所以你可以很容易地用LINQ语句来做:

IEnumerable<KeyValuePair<Guid, Object>> x = ...;
var newPairs = x.Select(kvp=> new KeyValuePair<string, object>(kvp.Key.ToString(), kvp.Value));