[英]Windows phone 8.1 Assembly.GetExecutingAssembly not available
[英]Use Assembly.GetExecutingAssembly() to get assembly from other class library
我使用實體框架在ObjectContext
類中映射數據庫中的類:
var typesToRegister = Assembly.GetExecutingAssembly().GetTypes()
.Where(type => !String.IsNullOrEmpty(type.Namespace))
.Where(type => type.BaseType != null && type.BaseType.IsGenericType && type.BaseType.GetGenericTypeDefinition() == typeof(EntityTypeConfiguration<>));
foreach (var type in typesToRegister)
{
dynamic configurationInstance = Activator.CreateInstance(type);
modelBuilder.Configurations.Add(configurationInstance);
}
//Replace with ....
//modelBuilder.Configurations.Add(new EntityMap());
上面的代碼獲取所有類typeof(EntityTypeConfiguration<>)
。 如果我的“ EntityMap”類與對象上下文位於同一類庫中,則可以,但是如果我的map classes
在單獨的類庫中編寫的,則無法使用。 無法在ObjectContext
上標識Map classes
。
我建議將其更改為:
var typesToRegister = typeof(MapClass).Assembly.GetTypes()
.....
其中MapClass
是從另一個程序集中的EntityTypeConfiguration<T>
繼承的類之一。
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
foreach (var assembly in assemblies)
{
assembly.GetTypes().Where(type => !String.IsNullOrEmpty(type.Namespace))
.Where(type => type.BaseType != null && type.BaseType.IsGenericType && type.BaseType.GetGenericTypeDefinition() == typeof(EntityTypeConfiguration<>));
foreach (var type in typesToRegister)
{
dynamic configurationInstance = Activator.CreateInstance(type);
modelBuilder.Configurations.Add(configurationInstance);
}
}
那應該讓您獲得所有已加載的程序集,而不僅僅是當前的程序集。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.