[英]C# reflection and Func<object,bool> member
I have 3 classes, each of which has the same function fields: 我有3个类,每个类具有相同的功能字段:
class A {
Func<sampleobject,bool> alpha = c=>c.some1 == something;
Func<sampleobject,bool> beta = c=>c.some1 == something;
}
class B {
Func<sampleobject,bool> alpha = c=>c.some1 == something;
Func<sampleobject,bool> beta = c=>c.some1 == something;
}
class C {
Func<sampleobject,bool> alpha = c=>c.some1 == something;
Func<sampleobject,bool> beta = c=>c.some1 == something;
}
I have a factory that will get me the proper class. 我有一家工厂,可以使我上适当的课。 There's also another method that will determine which function of the class gets called.
还有另一个方法将确定要调用类的哪个函数。
dbcontext.sampleobjects.where(Factory(class).ReflectionFunction(memberName));
I was able to use reflection to get the member name via: 我能够使用反射通过以下方式获取成员名称:
var prop = this.GetType().GetMember("alpha");
I just don't know how to use this or what to call to be able to be used in the "WHERE" statement like if I would call it directly. 我只是不知道如何使用该名称或如何调用才能在“ WHERE”语句中使用,就像我直接调用它一样。
dbcontext.sampleobjects.Where(instanceofA.alpha);
var prop = this.GetType().GetMember("alpha");
is a bit misleading - it is a field, not a property. 有点误导-它是一个字段,而不是属性。 However, you can get its actual value with:
但是,您可以通过以下方式获得其实际价值:
var func = (Func<sampleobject, bool>)prop.GetValue(this);
and then use it in the Where
call: 然后在
Where
调用中使用它:
dbcontext.sampleobjects.Where(func);
(or use it directly on an object like this:) (或直接在这样的对象上使用它:)
sampleobject obj = null; // or some other value
var result = func(obj);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.