如何使用 QueryOver 编辑 NHibernate Junction?
How to edit NHibernate Junction using QueryOver?
上下文:
我有一个方法可以创建一个长Conjunction
:
public static Conjunction GetLongConjunction()
{
Conjunction conjunction = new Conjunction();
conjunction.Add<Person>(p => p.Id > 0);
conjunction.Add<Person>(p => p.Age > 18);
Disjunction disjunction = new Disjunction();
disjunction.Add<Person>(p => p.Name == "John");
disjunction.Add<Person>(p => p.Name == "Alice");
conjunction.Add(disjunction);
// ...
return conjunction;
}
在另一种方法中,我使用 GetLongConjunction
来获得连词:
public void AnotherMethod()
{
Conjunction newConjunction = GetLongConjunction();
// ...
}
The problem is: I want to edit (or remove) one condition in newConjunction
.
我试过的:
我试图从 AbstractCriterion
class 中获取 criteria
属性 中的 List<NHibernate.Criterion.ICriterion>
。 Conjunction
扩展它:
Conjunction: Junction: AbstractCriterion
但是criteria
是一个private属性,我拿不到
问题:
所以,我的问题是:
如何编辑 NHibernate Junction?这可能吗?
谢谢!!
为简单起见,您可以使用反射:
public void AnotherMethod()
{
Conjunction newConjunction = GetLongConjunction();
IList<ICriterion> criteria = newConjunction.GetCriteria();
// Add or remove expressions
// var disjunction = (Disjunction) criteria.Last();
}
使用此扩展方法:
public static class MyExtensions
{
public static IList<ICriterion> GetCriteria(this Junction juntion)
{
return juntion.GetPrivateFieldValue<IList<ICriterion>>("criteria");
}
public static T GetPrivateFieldValue<T>(this object obj, string propName)
{
if (obj == null) throw new ArgumentNullException("obj");
Type t = obj.GetType();
FieldInfo fi = null;
while (fi == null && t != null)
{
fi = t.GetField(propName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
t = t.BaseType;
}
if (fi == null) throw new ArgumentOutOfRangeException("propName", string.Format("Field {0} was not found in Type {1}", propName, obj.GetType().FullName));
return (T)fi.GetValue(obj);
}
}
上下文:
我有一个方法可以创建一个长Conjunction
:
public static Conjunction GetLongConjunction()
{
Conjunction conjunction = new Conjunction();
conjunction.Add<Person>(p => p.Id > 0);
conjunction.Add<Person>(p => p.Age > 18);
Disjunction disjunction = new Disjunction();
disjunction.Add<Person>(p => p.Name == "John");
disjunction.Add<Person>(p => p.Name == "Alice");
conjunction.Add(disjunction);
// ...
return conjunction;
}
在另一种方法中,我使用 GetLongConjunction
来获得连词:
public void AnotherMethod()
{
Conjunction newConjunction = GetLongConjunction();
// ...
}
The problem is: I want to edit (or remove) one condition in
newConjunction
.
我试过的:
我试图从 AbstractCriterion
class 中获取 criteria
属性 中的 List<NHibernate.Criterion.ICriterion>
。 Conjunction
扩展它:
Conjunction: Junction: AbstractCriterion
但是criteria
是一个private属性,我拿不到
问题:
所以,我的问题是: 如何编辑 NHibernate Junction?这可能吗?
谢谢!!
为简单起见,您可以使用反射:
public void AnotherMethod()
{
Conjunction newConjunction = GetLongConjunction();
IList<ICriterion> criteria = newConjunction.GetCriteria();
// Add or remove expressions
// var disjunction = (Disjunction) criteria.Last();
}
使用此扩展方法:
public static class MyExtensions
{
public static IList<ICriterion> GetCriteria(this Junction juntion)
{
return juntion.GetPrivateFieldValue<IList<ICriterion>>("criteria");
}
public static T GetPrivateFieldValue<T>(this object obj, string propName)
{
if (obj == null) throw new ArgumentNullException("obj");
Type t = obj.GetType();
FieldInfo fi = null;
while (fi == null && t != null)
{
fi = t.GetField(propName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
t = t.BaseType;
}
if (fi == null) throw new ArgumentOutOfRangeException("propName", string.Format("Field {0} was not found in Type {1}", propName, obj.GetType().FullName));
return (T)fi.GetValue(obj);
}
}