简体   繁体   English

转换KeyValuePair <Guid, object> 到KeyValuePair <string, object>

[英]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.

相关问题 将对象转换为具有“通用值”的 KeyValuePair? - Cast object to KeyValuePair with "generic value"? C#-如何转换KeyValuePair <string,object> 到KeyValuePair <object,object> ? - C# - How can I cast KeyValuePair<string,object> to KeyValuePair<object,object>? C# 如何与 IEnumerable 类型的 object 联合<keyvaluepair<guid, string> &gt; </keyvaluepair<guid,> - C# how union with object of type IEnumerable<KeyValuePair<Guid, string>> KeyValuePair <string,object> Object Enumerable? - KeyValuePair<string,object> Is the Object Enumerable? 转换IEnumerable <KeyValuePair<string, object> &gt;对象到XML - convert an IEnumerable<KeyValuePair<string, object>> object to XML JSON序列化列表 <KeyValuePair<string, object> &gt; - JSON Serialize List<KeyValuePair<string, object>> 复制ObservableCollection <myclass> 到ObservableCollection <KeyValuePair<string,object> &gt; - Copy ObservableCollection<myclass> to ObservableCollection<KeyValuePair<string,object>> 将WPF TreeView控件绑定到KeyValuePair <string,object> - Binding a WPF TreeView control to a KeyValuePair<string,object> 绑定到ObservableCollection的“值” <KeyValuePair<object, string> &gt; - Bind to the “value” of ObservableCollection<KeyValuePair<object, string>> 将 object 转换为 KeyValuePair - Convert object to KeyValuePair
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM