c# - .NET generic types - finding most specific type -


Is there a good algorithm for determining "best" type to complete a request?

For example, say I have the following classes:

  Public interface ISOMTIP & lt; T & gt; {} Public Square SomeTypeImpl & lt; T & gt;: ISometype & lt; T & gt; {} Public class is a special type of IPLP & lt; T & gt;: ISometype & lt; T & gt; Where T: ISpecificSpecifier {} Public Interface ISpecificSpecifier {}  

Suppose a caller wants the best implementation type for this interface, I can apply this kind of special method: / P>

  Public Type GetBestImplementationType (type genericParam) {try {return typeof (SomeSpecificTypeImpl & lt;>) MakeGenericType (genericParam); } Hold (Logic Expression) {} Return Type (Some TypeImpl <> MakeGenericType (General Param); }  

This implementation will work for this particular case, but I am more concerned about generalization where more than one possible specific implementation and many generic parameters can be:

  public type GetBestImplementationType (type [] generic perm, type [] possible type) {foreach (potential type in var t) {try {return t.MakeGenericType (genericParams); } Hold (argument expression) {}} throw new exception ("unable to find specific implementation type"); }  

The given type array should be given this task at least with the specific order. So for the answer, either the method (or something quite similar) or the algorithm executing an algorithm, which I can use in this method. [Warning: code unchecked, syntax / logic errors may be present]

I think the only way to do this is to The sections have to be redone again which can be quite slow.

How to find asp net mvc for all controllers in this project:

  Private static list & lt; Type & gt; GetAllControllerTypes (IBuildManager buildManager) {// go through all the assemblies referenced by the application and search for // controllers and controller factories. & Lt; Type & gt; Controller type = new list & lt; Type & gt; (); ICONU Legislatures = BuildManager.GetreferenceSemblees (); Foreign (Legislative Assemblies) (type [] type INAsm; Try {typesInAsm = assembly.GetTypes ();} Hold (ReflectionTypeLoadException ex) {typesInAsm = ex.Types;} Controller type. Additional range (Type Inams. Where (IsControllerType) );} Return Controller Type;}  

In your case, you can work on a similar code again:

  Private Static List & Lt; type & gt; GetAllSubtypesOf (type interface) {list & lt; type & gt; type = new list & lt; type & gt; (); icullan assembly = BuildManager.GetreferenceSemblees (); Foreign (Legislative Assemblies) (Type [] Type INAsm; Try {typesInAsm = assembly.GetTypes ();} Hold (ReflectionTypeLoadException ex) {typesInAsm = ex.Types;} Types.AddRange (typesInAsm.Where (T = & gt; one interface. ISBN (T)));} Return type;}  

Note that because all sovereigns are quite unable to re-run asp.net MVC Do it once and cache the results.


Comments

Popular posts from this blog

MySql variables and php -

url rewriting - How to implement the returnurl like SO in PHP? -

Which Python client library should I use for CouchdB? -