【发布时间】:2011-02-07 11:48:15
【问题描述】:
只要我在与Program 相同的程序集中有类ClassSameAssembly,下面的代码就可以正常工作。
但是,当我将类 ClassSameAssembly 移动到单独的程序集时,会抛出 RuntimeBinderException(见下文)。
有可能解决吗?
using System;
namespace ConsoleApplication2
{
public static class ClassSameAssembly
{
public static dynamic GetValues()
{
return new
{
Name = "Michael", Age = 20
};
}
}
internal class Program
{
private static void Main(string[] args)
{
var d = ClassSameAssembly.GetValues();
Console.WriteLine("{0} is {1} years old", d.Name, d.Age);
}
}
}
Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'object' 不包含'Name' 的定义
at CallSite.Target(Closure , CallSite , Object )
at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0)
at ConsoleApplication2.Program.Main(String[] args) in C:\temp\Projects\ConsoleApplication2\ConsoleApplication2\Program.cs:line 23
【问题讨论】:
-
StackTrace: 在 CallSite.Target(Closure , CallSite , Object ) at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,Tret](CallSite site, T0 arg0) at ConsoleApplication2.Program.Main(String[ ] args) 在 C:\temp\Projects\ConsoleApplication2\ConsoleApplication2\Program.cs:第 23 行在 System.AppDomain._nExecuteAssembly(RuntimeAssembly 程序集,String[] args) 在 System.AppDomain.nExecuteAssembly(RuntimeAssembly 程序集,String[] args ) 在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
-
在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 在 System.Threading.ThreadHelper.ThreadStart_Context(Object state) 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart() InnerException:
-
有完整源代码的最终解决方案吗?
标签: dynamic c#-4.0 anonymous-types