How to handle a universal dictionary whose types are unknown and irrelevant?

If "value" is an incoming general dictionary whose types are unknown / do not matter, how can I take its entries and put them in the target type dictionary IDictionary<object, object>?

if(type == typeof(IDictionary<,>))
{
    // this doesn't compile 
    // value is passed into the method as object and must be cast       
    IDictionary<,> sourceDictionary = (IDictionary<,>)value;

    IDictionary<object,object> targetDictionary = new Dictionary<object,object>();

    // this doesn't compile
    foreach (KeyValuePair<,> sourcePair in sourceDictionary)
    {
         targetDictionary.Insert(sourcePair.Key, sourcePair.Value);
    }

    return targetDictionary; 
}

EDIT:

Thanks for the answers so far.

The problem here is that the copy argument is known only as the type 'object'. For instance:

public void CopyCaller(object obj) 
{ 
    if(obj.GetType() == typeof(IDictionary<,>) 
         Copy(dictObj); // this doesn't compile 
} 
+5
source share
5 answers

Create your general method, and then you can do what you do. You do not have to change the usage pattern, because the compiler will be able to derive common types from input types.

public IDictionary<object, object> Copy(IDictionary<TKey, TValue> source)
{

    IDictionary<object,object> targetDictionary = new Dictionary<object,object>();

    foreach (KeyValuePair<TKey, TValue> sourcePair in sourceDictionary)
    {
         targetDictionary.Insert(sourcePair.Key, sourcePair.Value);
    }

    return targetDictionary; 
}

IDictionary<TKey, TValue> IDictionary<object, object>, Dictionary<TKey, TValue>, - , .

+5

( , , ), /.

    private static Dictionary<object,object> DeTypeDictionary<T,U>(Dictionary<T,U> inputDictionary)
    {
        Dictionary<object, object> returnDictionary = new Dictionary<object, object>();
        foreach(T key in inputDictionary.Keys)
        {
            if( (key is object) && (inputDictionary[key] is object))
            {
                returnDictionary.Add(key, inputDictionary[key]);
            }
            else
            {
                //sorry these aren't objects. they may be dynamics.
                continue;
            }

        }
        return returnDictionary;
    }

... ...

        Dictionary<string, DateTime> d = new Dictionary<string, DateTime>();
        d.Add("rsgfdg", DateTime.Now);
        d.Add("gfdsgd", DateTime.Now);

        Dictionary<object, object> newDictionary = DeTypeDictionary<string, DateTime>(d);
+1

, , , :

  • ,

, , , :

class Bar
{
    public static void Foo<TKey, TValue>(Dictionary<TKey, TValue> input) { ... }
}

bool TryHandleDictionary(object o)
{
    var t = o.GetType();
    if (!t.IsGenericType || t.GetGenericTypeDefinition() != typeof(Dictionary<,>)) return false;

    var m = typeof(Bar).GetMethod("Foo", BindingFlags.Public | BindingFlags.Static);
    var m1 = m.MakeGenericMethod(t.GetGenericArguments());
    m1.Invoke(null, new[] { o });
    return true;
}
0

, IDictionary.

private static Dictionary<object, object> CreateCopy(IDictionary source)
{
    var copy = new Dictionary<object, object>(source.Count);
    foreach (DictionaryEntry entry in source)
    {
        copy.Add(entry.Key, entry.Value);
    }
    return copy;
}

:

var source = new Dictionary<int, string>() { { 1, "Foo" }, { 2, "Bar" }, };
var copy = CreateCopy(source);
Console.WriteLine(String.Join(", ", copy.Values));

:

,

0

This may be a problem for you, but to use the var keyword you will need .net 3.5 or more.

// this should compile
foreach (var sourcePair in sourceDictionary)
{
     targetDictionary.Insert(sourcePair.Key, sourcePair.Value);
}
-1
source

Source: https://habr.com/ru/post/1734193/


All Articles