0

I have a solution and multiple .net core projects.

  • Project (solution)
    • WebApi
    • Business
    • Data

WebApi project references Business, but does not reference Data project. So I want to get all types in solution that implements IMyInterface. All projects has classes that implements IMyInterface. I want to find all of them.

I am calling following code in WebApi project:

        var m = AppDomain.CurrentDomain.GetAssemblies()
            .SelectMany(name => name.GetTypes())
            .Where(type => typeof(IMyInterface).IsAssignableFrom(type))
            .ToList();

But not types found.

How can I find?

barteloma
  • 6,403
  • 14
  • 79
  • 173
  • you can't get types in projects. You can get them only in assemblies. – T.S. Apr 04 '20 at 18:44
  • @T.S. yes I mean in assemblies. Do you have any solution? – barteloma Apr 04 '20 at 18:48
  • **1.** answer provided below **2.** your question is still not fixed - it says "solution projects". **3.** `Directory.GetFiles... Assembly.Load`. **4.** google - "c# get assembly times that implement interface" – T.S. Apr 04 '20 at 18:56
  • Does this answer your question? [Getting all types that implement an interface](https://stackoverflow.com/questions/26733/getting-all-types-that-implement-an-interface) – T.S. Apr 04 '20 at 18:58

1 Answers1

1

You should get all assembly projects like this

public Assembly[] GetAssemblies()
{
      var dataAssembly = typeof(AnClassInDataLayer).Assembly;
      var businessAssembly = typeof(ACLassInBusinessLayer).Assembly;
      var webApiAssembly = typeof(Startup).Assembly;
      return new Assembly[] { businessAssembly , dataAssembly, webApiAssembly  };
}

then

var m = GetAssemblies()
        .SelectMany(name => name.GetTypes())// or .SelectMany(a => a.ExportedTypes)
        .Where(type => typeof(IMyInterface).IsAssignableFrom(type))
        .ToList();

Update

In .Net Core if WebApi project referenced business layer and Business layer reference the Data layer, so in WebApi layer you can access to Data layer

Farhad Zamani
  • 5,381
  • 2
  • 16
  • 41