Str*_*ior 19 c# generics dynamic c#-7.0
玩弄C#7的Local Functions,我最终得到了一些有趣的行为.考虑以下程序:
public void Main()
{
Console.WriteLine("Entered Main");
DoSomething("");
}
private void DoSomething(object obj)
{
Console.WriteLine("Entered DoSomething");
Generic((dynamic)obj);
GenericLocal(obj);
GenericLocal((dynamic)obj); // This breaks the program
void GenericLocal<T>(T val) => Console.WriteLine("GenericLocal");
}
private void Generic<T>(T val) => Console.WriteLine("Generic");
Run Code Online (Sandbox Code Playgroud)
这会产生:
进入Main
......然后抛出一个BadImageFormatException: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)
.堆栈跟踪:
at UserQuery.DoSomething(Object obj)
at UserQuery.Main()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
Run Code Online (Sandbox Code Playgroud)
(我在LINQPad中运行它,但是我从dotnetfiddle得到了类似的结果.)
删除代码中指示的行会产生您期望的输出:
输入主要
输入的DoSomething
Generic
GenericLocal
有谁能解释为什么?
归档时间: |
|
查看次数: |
674 次 |
最近记录: |