12

This is easy to get all available types (for some interface for example) in the old .NET, but I can't find the way how to do that in the new CoreCLR.

What I want to do is to have function like GetRepository, that should look for existing implementation of IRepository and return new instance of that type. Implementation will be located in the different project.

So, in .NET I can use something like this:

AppDomain.CurrentDomain.GetAssemblies().SelectMany(a => a.GetTypes())

The only solution I have for CoreCLR for now is:

public T GetRepository<T>()
{
  foreach (Type type in typeof(T).GetTypeInfo().Assembly.GetTypes())
    if (typeof(T).IsAssignableFrom(type) && type.GetTypeInfo().IsClass)
      return (T)Activator.CreateInstance(type);

  return default(T);
}

But it works only if interface and implementation are located in the same assembly (and this is not my case).

Thank you!

5
  • 1
    Can you please write in 1 line(comment), what is coreCLR and why would I need it ? ( I know what mscorelib is)
    – Royi Namir
    May 2, 2015 at 20:51
  • For me the most important is ability to run the web app on any platform (or in cloud, or even self-hosted, without web server), to have framework separated to the small packages that are updated independently. I think this is small revolution. You can read more at asp.net website. May 2, 2015 at 21:01
  • 1
    AppDomain.CurrentDomain.GetAssemblies() seems to be in CoreCLR too: see here
    – Christian
    May 2, 2015 at 22:24
  • I have found that too, but I can't find this class in System namespace or in some NuGet package. Also I've read that AppDomain is not supported. May 3, 2015 at 7:23
  • Aha I see. You could always ask your question in the CoreCLR chat room: gitter.im/dotnet/coreclr
    – Christian
    May 3, 2015 at 10:56

1 Answer 1

9

So, here is the answer from Microsoft: https://github.com/dotnet/coreclr/issues/919

In short, there is new

Microsoft.Framework.Runtime.LibraryManager

with

public IEnumerable<ILibraryInformation> GetLibraries();
public IEnumerable<ILibraryInformation> GetReferencingLibraries(string name);

etc

UPD: starting from RC2 use Microsoft.Extensions.DependencyModel.DependencyContext instead:

DependencyContext.Default.CompileLibraries
DependencyContext.Default.RuntimeLibraries
1
  • Doesn't seem like there is a replacement for GetReferencingLibraries(string name) in the new DependencyContext!!!
    – irperez
    Jan 6, 2017 at 20:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.