1

I have an application with multiple projects. Of course, each project create it's own dll. Then we can use AppDomain.CurrentDomain.GetAssemblies().ToList() to get all assemblies to use reflection for many reasons.

The AppDomain.CurrentDomain.GetAssemblies() is going to scan every assembly that exists including the standard assumblies like Microsoft's or packages we pull using Nuget of other dependency management tools. What if I want to only scan only my projects dll not the others?

Is there a way to give my projects a shared type then look for that shared type? Unfortunately my projects don't have a common name schema to scan the name for something that starts-with or ends-with here.

Agnel Amodia
  • 765
  • 8
  • 18
Junior
  • 11,602
  • 27
  • 106
  • 212
  • You can do exacly what you proposed. Declare a common custom attribute and apply that to all your assemblies. – thehennyy Mar 01 '18 at 15:17
  • Where would I declare that at? do you have an example? – Junior Mar 01 '18 at 15:20
  • Maybe it would be possible to go for [`AssemblyInfo.CompanyName`](https://msdn.microsoft.com/en-us/library/microsoft.visualbasic.applicationservices.assemblyinfo.companyname(v=vs.110).aspx) ? – Fildor Mar 01 '18 at 15:22
  • Just create a custom attribute type in an new assembly then reference and apply it in all you other assemblies. – thehennyy Mar 01 '18 at 15:23
  • @MikeA, if you pay me 500 bucks, i will let you in on the secret of how to [search Stackoverflow for useful information](https://stackoverflow.com/questions/1936953/custom-assembly-attributes) ;-) –  Mar 01 '18 at 15:24
  • you could always strong-name them with the same key? otherwise: an assembly-level attribute – Marc Gravell Mar 01 '18 at 15:24

2 Answers2

3

You can use one of built-in assembly attributes, for example CompanyName. Add this attribute to your assembly (or edit, usually it's already added in AssemblyInfo.cs file):

[assembly: AssemblyCompany("My company")]

Then check if company name matches:

static bool IsMyAssembly(Assembly asm) {
    var company = asm.GetCustomAttribute<AssemblyCompanyAttribute>();
    return company != null && company.Company == "My company";
}
Evk
  • 98,527
  • 8
  • 141
  • 191
0

You can filter the GAC assemblies by using:

AppDomain.CurrentDomain.GetAssemblies().Where(a => !a.GlobalAssemblyCache)

You can add a custom attribute to each assembly and query it:

AppDomain.CurrentDomain.GetAssemblies()
    .Select(a => a.GetCustomAttribute<MyAttribute>()).Where(attr => attr != null && attr.CustomProp == "MyValue")
Amir Popovich
  • 29,350
  • 9
  • 53
  • 99