.net C使用反射和linq将通用对象属性替换为另一个值
我不太明白这一点。为了简单起见,我会解释我想做什么 目前,我正在从事一个使用存储库模式的nHibernate项目。数据库中有一组具有描述字段的表。现在,我正在尝试本地化这些表以使用公共资源表 因此,我有两张表: 表1——资源: ResourceId、LanguageId、Value 表2-链接标签: LinkLabelId,描述,资源ID 然后有一个会话令牌,它包含一个LanguageId,这是用户的首选语言 好的,说到这里,我需要做的是从ResourcesTable中获取值,其中ResourceId from LinkLabels=ResourceId from Resources,用于具有ResourceId的对象。但我必须用泛型类型来完成 以下是我到目前为止的情况:.net C使用反射和linq将通用对象属性替换为另一个值,.net,nhibernate,c#-4.0,linq-to-objects,linq-to-nhibernate,.net,Nhibernate,C# 4.0,Linq To Objects,Linq To Nhibernate,我不太明白这一点。为了简单起见,我会解释我想做什么 目前,我正在从事一个使用存储库模式的nHibernate项目。数据库中有一组具有描述字段的表。现在,我正在尝试本地化这些表以使用公共资源表 因此,我有两张表: 表1——资源: ResourceId、LanguageId、Value 表2-链接标签: LinkLabelId,描述,资源ID 然后有一个会话令牌,它包含一个LanguageId,这是用户的首选语言 好的,说到这里,我需要做的是从ResourcesTable中获取值,其中Resourc
public IQueryable<T> ToQueryable<T>()
{
try
{
PropertyInfo resourceProperty = typeof(T).GetProperty("ResourceKey");
if (resourceProperty != null)
{
// Somthing like this, but of course this won't work
// because x.ResourceId doesn't exist because of generic, and the select
// statement is invalue.
//
// return from x in this.session.Query<T>()
// join p in this.session.Query<Resource>() on x.ResourceId equals p.ResourceId
// where p.LanguageId = 1 // this will be from a session object, hardcoded to 1 for now
// select x where x.Description is p.value;
}
else
{
return this.session.Query<T>();
}
}
catch (Exception ex)
{
throw ex;
}
}
创建如下界面: 接口IResource{ int ResourceId{get;set;} } 然后使用约束: 公共IQueryable可查询,其中T:IResource { ... }
然后,您必须使所有类型也实现IResource。是的,我也达到了这一点,希望它真正与对象断开连接,但关系设置的方式似乎不可能做到这一点。