当我运行下面的代码时,我得到RuntimeBinderException: 'object' does not contain a definition for 'SetIt'
。私人内部类动态失败
public interface IInput { }
public interface IThing<in TInput>
{
void SetIt(TInput value);
}
public class ThingMaker
{
private class Thing<TInput> : IThing<TInput>
where TInput : IInput
{
public void SetIt(TInput value){}
}
private class Input : IInput {}
public IThing<IInput> GetThing()
{
return new Thing<IInput>();
}
public IInput GetInput()
{
return new Input();
}
}
class Program
{
static void Main(string[] args)
{
var thingMaker = new ThingMaker();
var input = thingMaker.GetInput();
dynamic thing= thingMaker.GetThing();
thing.SetIt(input);
}
}
如果我切换thing
到var
它工作正常。所以很清楚,thing
有SetIt
。当我使用动态时,这怎么会失败?当它是dynamic
时,不应该作为var
工作的任何东西也能工作吗?
我认为它与Thing<TInput>
是私人内部类有关,因为它在我公开时起作用。