0

I have the following code in assembly A:

[AttributeUsage(AttributeTargets.Assembly)]
public class AssemblyCategoryAttribute : Attribute {
    public string Category { get; set; }

    public AssemblyCategoryAttribute(string category) {
        Category = category;
    }
}

Assembly B references assembly A and has this attribute applied. Finally assembly C also references assembly A and has the following code:

var assembly = Assembly.LoadFrom("..."); // Get assembly B
var description = assembly.GetCustomAttributes(false).OfType<AssemblyDescriptionAttribute>().SingleOrDefault();
var category = assembly.GetCustomAttributes(false).OfType<AssemblyCategoryAttribute>().SingleOrDefault();

However it gets the description fine but the category returns null. I'd appreciate it if someone could help me solve this. Thanks

Edit

As requested in the comments, here's my AssemblyInfo.cs file in assembly B:

using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using AssemblyANameSpace;

[assembly: AssemblyTitle("Name")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyCategory("Core")]

...
Jens Björnhager
  • 5,632
  • 3
  • 27
  • 47
nfplee
  • 7,643
  • 12
  • 63
  • 124

1 Answers1

0

I did the following:

  1. I created AssemblyA, and AssebmlyB, and WindowsFormApplication1
  2. I added reference of AssemblyA which contained AssemblyCategoryAttribute to AssemblyB and to WindowsFormApplication1
  3. I added AssemblyCategory attribute to AssemblyB

This seem to work for me:

string assemblyFile = @"D:\My Documents\Visual Studio 2008\Projects\ClassLibrary1\bin\x64\Debug\AssemblyB.dll";
byte[] assemblyBytes = File.ReadAllBytes(assemblyFile);

var assembly = Assembly.Load(assemblyBytes); // Get assembly B 
var description = assembly.GetCustomAttributes(false).OfType<AssemblyDescriptionAttribute>().SingleOrDefault(); 
var category = assembly.GetCustomAttributes(false).OfType<AssemblyCategoryAttribute>().SingleOrDefault();
Alex Mendez
  • 5,120
  • 1
  • 25
  • 23