相关文章推荐
逆袭的蘑菇  ·  【java框架】SpringMVC(4) ...·  2 周前    · 
淡定的地瓜  ·  iOS 通信通知 ...·  1 周前    · 
礼貌的凳子  ·  第四章 ...·  9 月前    · 
痛苦的企鹅  ·  No module named ...·  1 年前    · 
绅士的创口贴  ·  Workbook.Save 方法 ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I am using a Library with a class that have the following constructors:

public JobDataMap(IDictionary<string, object> map);
public JobDataMap(IDictionary map);

I created an instance of JobDataMap:

var jdm = new JobDataMap(new Dictionary<String, Object> { 
  { "Manager", myObject } 

But I am getting the compilation error:

The call is ambiguous between the following methods or properties:
'JobDataMap.JobDataMap(IDictionary<string, object>)' and 'JobDataMap.JobDataMap(IDictionary)' 

How to solve this?

You can enforce the type being passed like so:

var jdm = new JobDataMap((IDictionary<string, object>)new Dictionary<String, Object> { 
  { "Manager", myObject } 

Or you could make a factory method and make the non-generic constructor (I'm assuming you use this less) private:

public class JobDataMap
    public JobDataMap(IDictionary<string, object> map)
    private JobDataMap(IDictionary map)
    public static JobDataMap FromNonGenericMap(IDictionary map)
        return new JobDataMap(map);

Usage:

var jdm = JobDataMap.FromNonGenericMap(someNonGenericDictionary);

and then you can use the regular generic one like so:

var jdm = new JobDataMap(new Dictionary<String, Object> { 
  { "Manager", myObject } 

You can cast it to the type for the constructor you want it to use:

var jdm = new JobDataMap((IDictionary<string, object>) new Dictionary<String, Object> {
    { "Manager", new object() }

This design does seem a bit dubious, however...

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.