.NET Core 1.0, Enumerate All classes that implement the base class

I am working on porting an ASP.NET project to RC2. I use AutoFac to try to list the classes that use the base AutoMapper Profile class to configure all of my mapping profiles without calling them explicitly. Earlier in the old version of ASP.NET (even in RC1), I was able to use the following code:

public class AutoMapperModule : Module { protected override void Load(ContainerBuilder builder) { builder.RegisterAssemblyTypes().AssignableTo(typeof(Profile)).As<Profile>(); builder.Register(context => { var profiles = AppDomain.CurrentDomain.GetAssemblies() .SelectMany(IoC.GetLoadableTypes) .Where(t => t != typeof(Profile) && t.Name != "NamedProfile" && typeof(Profile).IsAssignableFrom(t)); var config = new MapperConfiguration(cfg => { foreach (var profile in profiles) { cfg.AddProfile((Profile)Activator.CreateInstance(profile)); } }); return config; }) .AsSelf() .As<IConfigurationProvider>() .SingleInstance(); builder.Register(c => c.Resolve<MapperConfiguration>().CreateMapper(c.Resolve)).As<IMapper>().InstancePerLifetimeScope(); builder.RegisterType<MappingEngine>().As<IMappingEngine>(); } } 

This worked fantastic until I tried to convert my project to RC2 using the new netcoreapp1.0 framework, but now I get a development-time error on AppDomain, stating that "AppDomain does not exist in the current context." I saw some suggestions about using ILibraryManager or DependencyContext for this, but I cannot figure out how to make any of them work. Any suggestions?

+6
source share
2 answers

.Net Core currently (1.0 RTM) does not support AppDomain.GetAssemblies() or a similar API. He will probably support it in 1.1.

Until then, if you need this feature, you will need to stick with net452 (i.e. the Net Framework) instead of netcoreapp1.0 .

+4
source

Will this work?

 var all = Assembly .GetEntryAssembly() .GetReferencedAssemblies() .Select(Assembly.Load) .SelectMany(x => x.DefinedTypes) .Where(type => typeof(ICloudProvider).IsAssignableFrom(type.AsType())); foreach (var ti in all) { var t = ti.AsType(); if (!t.Equals(typeof(ICloudProvider))) { // do work } } 
-one
source

All Articles