I
I
iRumba2021-12-24 07:56:33
.NET
iRumba, 2021-12-24 07:56:33

How to get the value of a ParameterExpression?

I need to upgrade Ef Core (5). There is some function in the request, which should not be translated into a database function, but turned into a different construct (Expression).
To do this, I intercepted the traversal of the entire expression through the substitution of EF services.
The problem is that the function takes some object as a parameter and in my visitor I get a MethodCallExpression with a ParameterExpression that contains the parameter name and type. And to build the correct expression, I need the value of this parameter.

Here is an example request code

var myObject = new SomeObject();

ctx.MyEntity
    .Where(x => x.MyFunction(myObject))
    .ToList();


and this very myObject is what I need in my visitor. If I connected the visitor directly in the request function, I could pass an additional parameter to it with the value of the object. But then you would have to attach a visitor to each request, and this is inconvenient, ugly and generally atta.

Therefore, in the registration of EF services, I replaced QueryTranslationPreprocessor with my own.

The constructor for this service takes QueryTranslationPreprocessorDependencies and QueryCompilationContext as parameters.
I tried to find some way to get my object through these objects, but I did not find it. Either I searched badly, or through them it is impossible.
Also, the first expression in the overridden QueryTranslationPreprocessor.Process(Expression) method is QueryRootExpression. I also tried to find some links to my object in it.

Also, I was able to prohibit broadcasting objects of the SomeObject type through IEvaluatableExpressionFilterPlugin. After that, this object began to come to my visitor as a MemberExpression. This is closer (possibly) as it is no longer just a parameter, but an expression containing the MemberInfo. And this object is a class field (formed from the function inside which the request is created). That is, to get Member.GetValue(), I need to pass an instance of an object of type Member.DeclaringType there. But I don't know where to get it either.

Probably, I explained the essence of the problem too complicated and abstractly. So I will answer the leading questions.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vlad, 2022-01-03
@KislyFan

It is not clear why such difficulties? Why not build Expression in advance and throw it into filters as needed?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question