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.