动态动作< T> :执行时无效的参数 [英] Dynamic Action<T> : Invalid Arguments when executed
问题描述
我有一个简单的继承模型:
public class Base {}
public class Derived:Base {public string Name {get;组;
和我的课程:
public sealed class TypeSwitch< T>
{
私人字典<类型,动态> _dict;
public TypeSwitch()
{
_dict = new Dictionary< Type,dynamic>();
}
public TypeSwitch< T>在其中K:T
{
_dict.Add(typeof(K),action))中添加< K>(Action< K>
返回这个;
}
public void Execute(T item)
{
var type = item.GetType();
var action = _dict [type];
action(item);
}
}
我运行它:
static void Main(string [] args)
{
var ts = new TypeSwitch< Base>();
ts.Add< Derived>(d => {Console.WriteLine(d.Name);});
Base b = new Derived {Name =Jonesopolis};
ts.Execute(b);
}
当我到达 action(item)
我得到一个 RuntimeBinderException
说
附加信息: System.Action< ConsoleApp.Derived>'有一些无效的参数
如果我可以使它工作,这将是非常光滑和有用的。有人向我解释我失踪了吗?有可能得到这个工作吗?
尝试另一个级别的lambdas。除了工作,我预计这将比使用动态
更快,即使有两个代理被调用。
public sealed class TypeSwitch< T>
{
私人字典< Type,Action< T>> _dict; //不再动态
public TypeSwitch()
{
_dict = new Dictionary< Type,Action< T>(); //不再动态
}
public TypeSwitch< T>其中K:T
{
_dict.Add(typeof(K),o => action((K)o));< K>(Action< K> //外lambda在调用内部lambda之前转换该值
返回此值;
}
public void Execute(T item)
{
var type = item.GetType();
var action = _dict [type];
action(item);
}
}
I have a lot of places in my decently sized project where I need to switch on a type. Obviously I can't do that in .NET (in a way simple enough to satisfy me), so I have to do a decent amount of casting. This code is the result of trying to hide some of that in a proof of concept.
I have a simple inheritence modeled:
public class Base { }
public class Derived : Base { public string Name { get; set; } }
and my class:
public sealed class TypeSwitch<T>
{
private Dictionary<Type, dynamic> _dict;
public TypeSwitch()
{
_dict = new Dictionary<Type, dynamic>();
}
public TypeSwitch<T> Add<K>(Action<K> action) where K : T
{
_dict.Add(typeof(K), action);
return this;
}
public void Execute(T item)
{
var type = item.GetType();
var action = _dict[type];
action(item);
}
}
and I run it with:
static void Main(string[] args)
{
var ts = new TypeSwitch<Base>();
ts.Add<Derived>(d => { Console.WriteLine(d.Name); });
Base b = new Derived { Name = "Jonesopolis" };
ts.Execute(b);
}
When I get to action(item)
I get a RuntimeBinderException
saying
Additional information: Delegate 'System.Action<ConsoleApp.Derived>' has some invalid arguments
This would be pretty slick and helpful if I could get it working. Can someone explain to me what I'm missing? Is it possible to get this working?
Try another level of lambdas. In addition to working, I expect this would be considerably faster than using dynamic
, even though there are two delegates being invoked.
public sealed class TypeSwitch<T>
{
private Dictionary<Type, Action<T>> _dict; // no longer dynamic
public TypeSwitch()
{
_dict = new Dictionary<Type, Action<T>>(); // no longer dynamic
}
public TypeSwitch<T> Add<K>(Action<K> action) where K : T
{
_dict.Add(typeof (K), o => action((K) o)); // outer lambda casts the value before calling the inner lambda
return this;
}
public void Execute(T item)
{
var type = item.GetType();
var action = _dict[type];
action(item);
}
}
这篇关于动态动作< T> :执行时无效的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!