Why this dynamic parameter is not working?
c#, dynamic
Solution
Your code is using an anonymous type. Originally intended to be useful in a Linq select query expression, added in C# version 3. Unfortunately, the C# compiler generates them with the accessibility modifier for the members in the anonymous type set to internal. Accessibility is enforced by the CLR. Not an issue in Linq since the iterator will be generated in the same assembly as well. Using them with the dynamic keyword was not envisioned, it was added in a later C# version, version 4.
Which means that it will work fine as long as the code is all in the same assembly. However, as soon as you pass the object to code in another assembly, the restrictions enforced by internal kick in and it goes kaboom.
Simply declaring a little public class is the workaround. ExpandoObject works too but adds unnecessary overhead.
Problem
Consider this code in a project: ``` static void Main(string[] args) { DoSomething(new { Name = "Saeed" }); } public static void DoSomething(dynamic parameters) { Console.WriteLine(parameters.Name); } ``` This works like a charm. However, as soon as you separate these two functions into two different projects, the code breaks: ``` // This code is in a Console Application static void Main(string[] args) { ExternalClass.DoSomething(new { Name = "Saeed" }); } // However, this code is in a Class Library; Another project public class ExternalClass { public static void DoSomething(dynamic parameters) { Console.WriteLine(parameters.Name); } } ``` The error I get in the second case is: object' does not contain a definition for 'Name' (RuntimeBinderException) Why do I get this error? What's the alternative method? How can I pass a dynamic parameter to a method in another library, and use it there in a simple way? Note: I'm familiar with `ExpandoObject` and I don't want to use that.