我想编写一个带有签名 Expression> 的方法Foo()
。我的类 U 继承自 T。我想在此委托中包含特定于 U 的处理。问题是,T 不能隐式转换为 U。有什么方法可以在此方法中访问特定于 U 的属性吗?
复杂性:我还有类型 V : T 想要处理,所以我不能仅仅通过在签名中用 U 替换 T 来利用方差。
I want to write a method with signature Expression<Func<T, bool>> Foo<T>()
. My class U inherits from T. I want to include U-specific processing in this delegate. The problem is, T can't be implicitly converted to U. Is there any way I can access U-specific properties in this method?
Complication: I also have type V : T which I want to be handled, so I can't just take advantage of variance by replacing T with U in the signature.
发布评论
评论(2)
一种简单的方法是定义一个接口,T 和 U 都从中继承(使用它)。
A simple approach is to define an interface which both T and U inherit(Use it) from it.
我最终使用了单独的重载: (), (myVar) where T : U 和 (myvar, myvar2) where T : V 。这成功了,我很幸运,因为每个参数都是与实体属性对应的应用程序值。
I ended up using separate overloads: (), (myVar) where T : U, and (myvar, myvar2) where T : V. That did the trick and I lucked out because each parameter is an app value that corresponds with an entity property.