Created
December 16, 2016 15:41
-
-
Save neilgaietto/fa564fc29334d90bec7070cf2aabedd0 to your computer and use it in GitHub Desktop.
Registering all profiles for Automapper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Searches solution for all assemblies | |
public static class AppAssemblies | |
{ | |
public static IEnumerable<Assembly> AsEnumerable() | |
{ | |
string baseDirectory = AppDomain.CurrentDomain.BaseDirectory; | |
foreach (Assembly assembly in Directory.GetFiles(baseDirectory) | |
.Where(x => Path.GetExtension(x).Equals(".dll", StringComparison.OrdinalIgnoreCase)) | |
.Select(Assembly.LoadFrom)) | |
{ | |
yield return assembly; | |
} | |
string binPath = AppDomain.CurrentDomain.SetupInformation.PrivateBinPath; | |
if (!Directory.Exists(binPath)) | |
{ | |
yield break; | |
} | |
foreach (Assembly assembly in Directory.GetFiles(binPath) | |
.Where(x => Path.GetExtension(x).Equals(".dll", StringComparison.OrdinalIgnoreCase)) | |
.Select(Assembly.LoadFrom)) | |
{ | |
yield return assembly; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Call on startup | |
public static class MapperConfig | |
{ | |
public static void Register() | |
{ | |
foreach (Profile profile in AppAssemblies.AsEnumerable() | |
.Where(a => a.FullName.StartsWith("App"))//filter to only your projects assemblies | |
.SelectMany(a => a.GetTypes().Where(t => typeof(Profile).IsAssignableFrom(t))) | |
.Select(Activator.CreateInstance) | |
.Cast<Profile>()) | |
{ | |
Mapper.AddProfile(profile); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment