-1

this code works for me but there must be a better way to accomplish the same thing. The method Search exists in multiple namespaces. the correct namespace to use depends on code that is irrelevant just like some of the other code displayed. any thoughts on rewritting this better?

example namespaces used

  • MTG.Sites.Test1

  • MTG.Sites.Test2

     static public async Task<List<Card>> Search(string sNamespace)
     {
         List<Card> rawCards = null;
    
         try
         {
             Type t = Assembly.GetExecutingAssembly().GetType($"MTG.Sites.{sNamespace}");
             if (t != null)
             {
                 dynamic classInstance = Activator.CreateInstance(t);
    
                 rawCards = await classInstance.Search(httpClient);
             }
         }
         catch(Exception ex)
         {
             log.Error(ex);
         }
    
         return rawCards;
     }
    

the code i want to improve is the use of Assembly.GetExecutingAssembly().GetType("");

Aaron. S
  • 467
  • 4
  • 12

1 Answers1

0

the short answer is to use AutoFac. the longer answer is a complete rewrite of how the code is used. i essentially need to register each Search class that exists in each Namespace that contains one. then using the AutoFac examples (AttributeMetadataExample & AspNetCoreExample), i was able to eliminate the need for reflection to find the Search method in each Namespace (passed as a string to GetType)

Aaron. S
  • 467
  • 4
  • 12