0

I'm trying to get all types with the code below based on these topics:

So I have this attribute:

public class DataLayerInterfaceAttribute   : System.Attribute
{
    public DataLayerInterfaceAttribute() { }
}

And I have this interface:

[DataLayerInterfaceAttribute]
interface IInterface
{
    void Method(string param);
}

Finally I want to get the interface type:

var types = from type in assembly.GetTypes()
            where Attribute.IsDefined(type, typeof(DataLayerInterfaceAttribute))
            select type;

I already tested with this:

var types = from type in assembly.GetTypes()
            where type.GetCustomAttributes(type,true).Length > 0
            select type;

But the result for both is empty and I dont know why.

EDIT

I'm getting the assembly like this:

string assemblyName = System.Reflection.Assembly.GetExecutingAssembly().Location;
byte[] assemblyBytes = File.ReadAllBytes(assemblyName);
Assembly assembly = Assembly.Load(assemblyBytes);
Community
  • 1
  • 1
  • 2
    Your code works fine for me. Make sure you are using appropriate `assembly` – Sergey Berezovskiy Sep 27 '13 at 14:19
  • 2
    My crystal ball says that you are using the wrong Assembly reference. – Hans Passant Sep 27 '13 at 14:19
  • Why didn't you just write `Assembly assembly = Assembly.GetExecutingAssembly();`? Either way, the currently executing assembly is probably not what you want. Are all your interfaces going to be in the same assembly or do you know the list of assemblies that they will be located in? – Trevor Elliott Sep 27 '13 at 15:29

0 Answers0