Ask a questionAsk a question
 

AnswerAdding new assembly to catalog?

  • Tuesday, October 20, 2009 7:57 PMsaxisa Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Right now I have something like this:
    var catalog = new AggregateCatalog();
    var assemblyCatalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());
    catalog.Catalogs.Add(assemblyCatalog);

    But to add a new project assembly, is the only way to do something like this?
    catalog.Catalogs.Add(System.Reflection.Assembly.GetAssembly(typeof(MyProject.MyClass)));

    Seems fragile, what if I delete MyClass?  Is there a better way?

Answers

  • Tuesday, October 20, 2009 8:16 PMReed Copsey, Jr. Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    You have a few options.

    You can use Assembly.GetReferencedAssemblies to get a list of all of the assemblies referenced from your executing code.  You can filter this (maybe just take the ones that start with your company name) and add all of those, in order to add in all of the assemblies you have referenced.

    You can use a type to get an assembly, as mentioned above.  That's fragile, although not too bad, since removing the type (MyClass) will cause a compile-time error, so it's easily fixed.

    The other option si to use a DirectoryCatalog to load all of the assemblies sitting in a Directory.  This works great for runtime discovery of plugins.
    Reed Copsey, Jr. - http://reedcopsey.com
    • Marked As Answer bysaxisa Tuesday, October 20, 2009 8:24 PM
    •  

All Replies

  • Tuesday, October 20, 2009 8:16 PMReed Copsey, Jr. Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    You have a few options.

    You can use Assembly.GetReferencedAssemblies to get a list of all of the assemblies referenced from your executing code.  You can filter this (maybe just take the ones that start with your company name) and add all of those, in order to add in all of the assemblies you have referenced.

    You can use a type to get an assembly, as mentioned above.  That's fragile, although not too bad, since removing the type (MyClass) will cause a compile-time error, so it's easily fixed.

    The other option si to use a DirectoryCatalog to load all of the assemblies sitting in a Directory.  This works great for runtime discovery of plugins.
    Reed Copsey, Jr. - http://reedcopsey.com
    • Marked As Answer bysaxisa Tuesday, October 20, 2009 8:24 PM
    •  
  • Tuesday, October 20, 2009 8:24 PMsaxisa Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Cool, thanks Reed!