Skip to content

Instantly share code, notes, and snippets.

@maxfridbe
Created November 15, 2012 17:38
Show Gist options
  • Save maxfridbe/4080000 to your computer and use it in GitHub Desktop.
Save maxfridbe/4080000 to your computer and use it in GitHub Desktop.
AutoFac Modules dll loading
public static class ModuleBootstrapper
{
public static string GetModuleTitle(IViewModel instance)
{
var title = from a in instance.GetType().GetCustomAttributes(typeof(PublishedViewAttribute), false)
let pva = a as PublishedViewAttribute
select pva.Title;
return title.FirstOrDefault();
}
public static Uri GetCurrentUICultureSource(string resourceName, Type containingModule)
{
var name = containingModule.Assembly.GetName().Name;
var cult = Thread.CurrentThread.CurrentUICulture.ToString();
var location = cult == "en-US"
? string.Format("pack://application:,,,/{0};component/Resources/{1}.xaml", name, resourceName)
: string.Format("pack://application:,,,/{0};component/Resources/{1}.{2}.xaml", name, resourceName, cult);
var uri = new Uri(location, UriKind.RelativeOrAbsolute);
return uri;
}
public static ResourceDictionary GetCurrentUICultureResourceDictionary(string resourceName, Type containingModule)
{
var dict = new ResourceDictionary();
var uri = GetCurrentUICultureSource(resourceName, containingModule);
dict.Source = uri;
return dict;
}
public static IEnumerable<Type> LoadModulesPublishedTypes()
{
string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
string pathModules = Path.Combine(path, "modules");
return LoadModulesPublishedTypes(new DirectoryInfo(pathModules));
}
public static IEnumerable<Type> LoadModulesPublishedTypes(DirectoryInfo modulesDirectory)
{
var viewModelTypes = new List<Type>();
if (!modulesDirectory.Exists)
return new List<Type>();
var modules = modulesDirectory.GetFiles("*.dll");
foreach (var module in modules)
{
Assembly loadedAssembly = Assembly.LoadFile(module.FullName);
Type viewModel = typeof(IViewModel);
Type publish = typeof(PublishedViewAttribute);
var types = loadedAssembly.GetTypes()
.Where(t => viewModel.IsAssignableFrom(t)
&& t.GetCustomAttributes(publish, true)
.Any());
viewModelTypes.AddRange(types);
}
return viewModelTypes;
}
public static List<IModule> GetAutoFacModules()
{
string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
string pathModules = Path.Combine(path, "modules");
var imodules = new List<IModule>();
var modulesDirectory = new DirectoryInfo(pathModules);
if (!modulesDirectory.Exists)
return new List<IModule>();
var modules = modulesDirectory.GetFiles("*.dll");
foreach (var module in modules)
{
Assembly loadedAssembly = Assembly.LoadFile(module.FullName);
Type viewModel = typeof(IModule);
var types = loadedAssembly.GetTypes()
.Where(viewModel.IsAssignableFrom)
.Select(t => (IModule)Activator.CreateInstance(t));
imodules.AddRange(types);
}
return imodules;
}
public static void LoadCulturalDictionary(Collection<ResourceDictionary> mergedDictionaries, string stringresources, Type getType)
{
var name = CultureInfo.CurrentUICulture.Name;
if (name != "en-US")
{
var existing = mergedDictionaries.FirstOrDefault(rd => rd.Source.OriginalString.Contains(stringresources));
if (existing != null)
mergedDictionaries.Remove(existing);
var dict = ModuleBootstrapper.GetCurrentUICultureResourceDictionary(stringresources, getType);
mergedDictionaries.Add(dict);
}
}
public static void ChangeLanguage(string code)
{
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(code);
if(LanguageChanged != null)
LanguageChanged(code);
}
public static event Action<string> LanguageChanged;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment