背景使用的PropertyInfo为泛型类型时调用一个泛型方法
我使用的是EF,我有许多表。当我插入一个带有导航属性'内容但没有id的新实体时(我正在从xls文件中读取内容),我不想显式加载所有的导航属性。这是太多的代码。所以我尝试了一种通用的方式:
private void loadExistingNavigationProperties<TEntity>(TEntity entityToInsert) where TEntity : class
{
Type type = typeof(TEntity);
var properties = type.GetProperties().Except(type.GetProperties().Where(x => x.Name.Contains("id")));
foreach (PropertyInfo property in properties)
{
if (property.PropertyType.FullName.Contains("MyNamespace"))
{
property.SetValue(entityToInsert, findNavigationProperty<???>(property.GetValue(entityToInsert)));
}
}
}
我有我的entityToInsert
。如果它有一个导航属性(contains("MyNamespace")
),我检查它的所有属性。如果这是真的,导航属性应该加载(见下文)并设置。
private object findNavigationProperty<TNavigationProperty>(TNavigationProperty navigationPropertyValue) where TNavigationProperty : class
{
List<TNavigationProperty> navigationProperties = GetAllEntries<TNavigationProperty>();
foreach (var entity in navigationProperties)
{
if (propertiesAreEqual(entity, navigationPropertyValue))
{
return entity;
}
}
return navigationPropertyValue;
}
导航属性属性的当前值被传递。它包含所有信息,如名称或其他内容,但不包含id。首先,我将获取具有该类型的所有可用导航属性。然后我搜索是否有一个属性与当前属性相同。然后这个返回并设置为导航属性。
编辑:
public List<TEntity> GetAllEntries<TEntity>() where TEntity : class
{
using (var dbContext = new InventarDBEntities(MainWindow.connectionName))
{
return GetAllEntries<TEntity>(dbContext);
}
}
public List<TEntity> GetAllEntries<TEntity>(InventarDBEntities dbContext) where TEntity : class
{
return dbContext.Set<TEntity>().ToList();
}
问题
我的问题是,现在我该怎么告诉方法findNavigationProperty
的通用类型为类型,该属性的值了。因此用类型替换???
。
您无法在运行时检索泛型类型。泛型类型必须在编译期间可解析。如果您想将运行时解析类型传递给方法,则必须通过传递反射的System.Type来实现。 GetAllEntries()是否有一个接受“System.Type”参数的重载? –
@NoelWidmer我添加了GetAllEntries方法。它不接受System.Type的参数 – L3n95
你是否也有'InventarDBEntities'的来源?我想它是一个.NET类型。在这种情况下,你能告诉我它所源自的类名的名字吗? (我想看看是否有一个'InventarDBEntities.Set()'重载,它接受'System.Type'而不是泛型参数) –