将KeyValuePair <guid,object =“”>转换为KeyValuePair <string,object =“”> </string,> </guid,>

时间:2014-06-25 14:37:17

标签: c# .net

最简单的投射方式是什么:

IList<KeyValuePair<Guid, object>>

IList<KeyValuePair<string, object>>

没有循环等。

4 个答案:

答案 0 :(得分:3)

您无法在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();

答案 1 :(得分:1)

你可以创建一个包装IList类,它将在没有循环的情况下完成 - 但你必须自己提供转换(derived-parent,parent-derived)。

用法:

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

答案 2 :(得分:0)

如果你有KeyValuePair<Guid, object> k,你可以这样做:

new KeyValuePair<string, object>(k.Key.ToString(), k.Value);

答案 3 :(得分:0)

当你说:

  

没有循环等。

这是否意味着您有多个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));

这至少很干净......