<分区>
在项目中考虑这段代码:
static void Main(string[] args)
{
DoSomething(new { Name = "Saeed" });
}
public static void DoSomething(dynamic parameters)
{
Console.WriteLine(parameters.Name);
}
这就像一个魅力。但是,一旦您将这两个函数分离到两个不同的项目中,代码就会中断:
// 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);
}
}
我在第二种情况下得到的错误是:
object' does not contain a definition for 'Name' (RuntimeBinderException)
为什么会出现此错误?有什么替代方法?如何将动态参数传递给另一个库中的方法,并以简单的方式在那里使用它?
注意:我熟悉 ExpandoObject
,我不想使用它。