c# - Can you use Generic Action<> or Func<> parameters with the where clause? -


I'm not sure whether I'm missing something or just can not understand the way in which work ... actually I want to be able to take any action with a constraint parameter without having to reboot it, work with the code below with the cast:

  Private Readonly Dictionary & Lt; Type, action & lt; ICommand & gt; & Gt; _dictionary = New Dictionary & lt; Type, verb & lt; ICommand & gt; & Gt; (); Public Zero Register & lt; TCommand & gt; (Action & lt; TCommand & gt; Actions) where TComand: Icosmod {_dictionary.Add (Typef (Tecmund), X => Action ((Comm. X)); }  

The following triggers an error and says that no parameter of encoding can be passed as ICommand

  Public Zero Register & lt; TCommand & gt; (Action & lt; TCommand & gt; Action) Where Tecmund: ICMMund {_dictionary.Add (typed, action); }  

Am I doing something wrong or is it that the where constraint is only understood by the method signing and the remaining code ignores this instruction ?

The first common argument of

action is contravariant < / Em> not covariant Action & lt; ICommand & gt; a sub-type of action & lt; SomeCommand & gt; is not. In fact, this is the second way. Action & lt; SomeCommand & gt; is a sub type action & lt; ICommand & gt; !

Now you have such an action that only take a a few commissions . If you give it a action & lt; ICommand & gt; , then anyone can be from EvilCommand , but EvilCommand is not a type of SomeCommand . On the other hand, if you have written a method that can accept any type of ICommand , then obviously you can show that this was a method that only SomeCommand < / Code> because the address will implement them all to ICommand .

Your first solution works because you present the obvious artists of the parameter. You are creating a new method , which requires a specific type of investigation, apart from working, this is also the right design in this specific case; This is one of those rare situations where you know that the object is actually a TCommand example, not just a ICommand , but compiler Do not have to prove it, and there is no good way to redesign the application, which can verify it steadily.


Comments

Popular posts from this blog

import - Python ImportError: No module named wmi -

Editing Python Class in Shell and SQLAlchemy -

c# - MySQL Parameterized Select Query joining tables issue -