C# 如何将一个字典变量用于两种不同的类型?

C# 如何将一个字典变量用于两种不同的类型?,c#,dictionary,C#,Dictionary,我对C语言很陌生,想用字典 我的代码如下: public Task<IHttpActionResult> GetProgress(int id) { if(obj1) { Dictionary<string, object1> progresses = new Dictionary<string, object1> (); } else if(obj2) { Dictionary<str

我对C语言很陌生,想用字典

我的代码如下:

public Task<IHttpActionResult> GetProgress(int id)
{
   if(obj1)
   {
      Dictionary<string, object1> progresses = new Dictionary<string, object1> 
      ();
   }
   else if(obj2)
   {
      Dictionary<string, object2> progresses = new Dictionary<string, object2> 
      ();
   }
   return progresses;
}
   Object progresses = null;
   if(obj1)
   {
      progresses = new Dictionary<string, object1>();
   }
   else if(obj2)
   {
      progresses = new Dictionary<string, object2>();
   }
   return progresses;
    public Dictionary<string, object> GetProgress(int id)
    {
        Dictionary<string, object> returnValue;
        if(obj1)
        {
            Dictionary<string, object1> progresses = new Dictionary<string, object1>();
        }
        else if(obj2)
        {
            Dictionary<string, object2> progresses = new Dictionary<string, object2>();
        }
        return progresses;
    }
public任务GetProgress(int-id)
{
如果(obj1)
{
字典=新字典
();
}
else if(obj2)
{
字典=新字典
();
}
返回进度;
}
在这种情况下,我得到了错误,因为我没有在if-else之前定义变量

我试过这样做:

public Task<IHttpActionResult> GetProgress(int id)
{
   if(obj1)
   {
      Dictionary<string, object1> progresses = new Dictionary<string, object1> 
      ();
   }
   else if(obj2)
   {
      Dictionary<string, object2> progresses = new Dictionary<string, object2> 
      ();
   }
   return progresses;
}
   Object progresses = null;
   if(obj1)
   {
      progresses = new Dictionary<string, object1>();
   }
   else if(obj2)
   {
      progresses = new Dictionary<string, object2>();
   }
   return progresses;
    public Dictionary<string, object> GetProgress(int id)
    {
        Dictionary<string, object> returnValue;
        if(obj1)
        {
            Dictionary<string, object1> progresses = new Dictionary<string, object1>();
        }
        else if(obj2)
        {
            Dictionary<string, object2> progresses = new Dictionary<string, object2>();
        }
        return progresses;
    }
Object=null;
如果(obj1)
{
进步=新字典();
}
else if(obj2)
{
进步=新字典();
}
返回进度;
然而,它没有起作用。“progress.ContainsKey()”和“progress.Add()”出现错误

我怎样才能解决这个问题?
我提前表示感谢。

在C中没有像Java中那样的菱形操作符,允许访问封闭类型下面的开放泛型类型,从而防止泛型多态性


只能使用Dictionary类实现的非泛型接口。

如果要使用可以采用两种不同类型的泛型字典,它们需要基于相同的基类;否则,您必须使用非通用的


除此之外,示例中的代码没有多大意义。首先,由于要返回IHttpActionResult任务,因此不能在此处返回字典。您要么必须返回一个新任务(例如,通过
Task.Factory.StartNew()
),要么等待返回IHttpActionResult并使用async/await语法的内容。

我突然想到的是,您正试图将字典作为任务返回。这是您可能遇到的主要错误,也是为什么不能使用ContainsKey()或Add(),因为它们不是任务对象的属性。我们也不知道object1/obj1和object2/obj2是什么。我同意其他人的看法,你应该明确说明你的目标是什么

但是,由于不知道这个方法是什么,或者它的用途是什么,并且假设(总是一个糟糕的想法),obj1和obj2实际上存在于全局范围中,我会这样修改它:

public Task<IHttpActionResult> GetProgress(int id)
{
   if(obj1)
   {
      Dictionary<string, object1> progresses = new Dictionary<string, object1> 
      ();
   }
   else if(obj2)
   {
      Dictionary<string, object2> progresses = new Dictionary<string, object2> 
      ();
   }
   return progresses;
}
   Object progresses = null;
   if(obj1)
   {
      progresses = new Dictionary<string, object1>();
   }
   else if(obj2)
   {
      progresses = new Dictionary<string, object2>();
   }
   return progresses;
    public Dictionary<string, object> GetProgress(int id)
    {
        Dictionary<string, object> returnValue;
        if(obj1)
        {
            Dictionary<string, object1> progresses = new Dictionary<string, object1>();
        }
        else if(obj2)
        {
            Dictionary<string, object2> progresses = new Dictionary<string, object2>();
        }
        return progresses;
    }
公共字典GetProgress(int-id)
{
字典返回值;
如果(obj1)
{
Dictionary progress=newdictionary();
}
else if(obj2)
{
Dictionary progress=newdictionary();
}
返回进度;
}

尽管这可能会失败,因为您无法隐式地将字符串字典、对象转换为字符串字典,“Object1”,不管是什么…

这段代码有很多问题。让我们逐一检查一下

public Task<IHttpActionResult> GetProgress(int id)
{
   if(obj1)
   {
      Dictionary<string, object1> progresses = new Dictionary<string, object1> 
      ();
   }
   else if(obj2)
   {
      Dictionary<string, object2> progresses = new Dictionary<string, object2> 
      ();
   }
   return progresses;
}
现在编译器很高兴,但您在两种情况下都返回了相同类型的字典。我们怎样才能修复它?如果将这种情况归结为一种情况,则根据您的条件,您希望返回
字符串
Object1
Object2
的实例。处理这个问题的一种方法是向
Object1
Object2
声明一个父类,并创建该父类的字典

public class ParentObject
{
}

public class Object1 : ParentObject
{
}

public class Object2 : ParentObject
{
}
然后,创建字典,如下所示:

Dictionary<string, ParentObject> progresses = new Dictionary<string, ParentObject>();
当您调用该方法并想知道接收到的对象的类型时,可以按如下方式获取该对象的
类型

var result1 = GetProgress(1);
// Here, type = Object1
var typeOfObject1 = result1.First().Value.GetType();

var result2 = GetProgress(2);
// Here, type = Object2
var typeOfObject2 = result2.First().Value.GetType();

要扩展上一个伟大的答案,您可以使用

    (ParentObject)Activator.CreateInstance(Type.GetType($"Object{id}"))
您还可以将Object1和Object2基于接口,让它们实现相同的方法,例如:

    public interface ParentObject
    {
            int Id { get; set; }
            void ObjectAction(int value);
    }

    public class Object1 : ParentObject
    {
        public int Id { get; set; }
        public void ObjectAction(int value)
        {
            Console.WriteLine("I'm object #1 with value" + value);
        }
    }

    public class Object2 : ParentObject
    {
        public int Id { get; set; }
        public void ObjectAction(int value)
        {
            Console.WriteLine("I'm different object #2 with value" + value);
        }
    }

    public class UsingDifferentObjects
    {
            public Dictionary<string, ParentObject> GetProgress(int id)
            {
                    Dictionary<string, ParentObject> progresses = new Dictionary<string,ParentObject>();
                    var objtoAdd = (ParentObject)Activator.CreateInstance(Type.GetType($"Object{id}"));
                    objtoAdd.Id = id;
                    progresses.Add(id.ToString(), objtoAdd);
                    return progresses;
            }
    }

不,你不能那样做。您不能假装
Int32
字典是字符串字典,因为当您将字符串添加到
Int32
字典时,它不能包含这些字符串,并且会引发异常。请回想一下在你考虑创建这两本词典之前的那一刻。你试图解决一个问题,你认为这两本字典就是解决办法。第一个问题是什么,早期的,原始的问题,导致你现在的问题?你能在你的问题中用令人难以置信的详细信息和代码告诉我们这个问题吗?你可能也需要让处理这个问题的代码变得泛型,但是从你在这里展示的内容很难判断。使用此代码您到底想完成什么。GetProgress返回任务。但是您在实现中返回一个字典?