0

Right now, I'm doing this

var data = new JobDataMap(new Dictionary<string,string> { {"obj", "stringify"} });

But I want to do this:

dynamic d = new { obj = "stringify" };
var data = new JobDataMap(d);

Is there some secret syntactical sugar that would allow me to do this?

Caleb Jares
  • 6,163
  • 6
  • 56
  • 83
  • No it is not possible like that. But why don't you add your dynamic object to the JobDataMap, with a certain key? – Styxxy May 14 '13 at 23:00
  • Perhaps you can use [`RouteValueDictionary`](http://msdn.microsoft.com/en-us/library/cc680272.aspx`). – Silvermind May 14 '13 at 23:11

1 Answers1

1

There's no magical way of doing this. There's no way the compiler can know that your Dynamic object really is a Dictionary at compile time.

That being said, you could create an extension method that converts it to a Dictionary so that you could do something like this:

dynamic d = new { obj = "stringify" };
var data = new JobDataMap(d.ToDictionary());

This blogpost offers an example: http://blog.andreloker.de/post/2008/05/03/Anonymous-type-to-dictionary-using-DynamicMethod.aspx

Kenneth
  • 28,294
  • 6
  • 61
  • 84