declaring type of virtual property does not match to type of lambda parameter #48875
Replies: 13 comments
-
Closing out as the behavior seems correct to me. |
Beta Was this translation helpful? Give feedback.
-
but according to https://docs.microsoft.com/en-us/dotnet/api/system.reflection.memberinfo.declaringtype?view=netcore-3.1 typeof(Derived).GetProperty(nameof(Derived.Value)).DeclaringType = typeof(Derived) we'll get true |
Beta Was this translation helpful? Give feedback.
-
and another example - if we manually create this expression //workaround
var param = Expression.Parameter(typeof(Derived), "x");
var body = Expression.Property(param, nameof(Derived.Value));
var expr = Expression.Lambda<Func<Derived, int>>(body, param);
PropertyBelongsToParameter(expr); //returns true |
Beta Was this translation helpful? Give feedback.
-
Yes. because you're asking the decraring type of Derived.Value there. THat's not what C# has there. When you access |
Beta Was this translation helpful? Give feedback.
-
Note: you can see this clearly in the IL produced:
We're getting the method given the handle for |
Beta Was this translation helpful? Give feedback.
-
Thanks for explanation. var param = Expression.Parameter(typeof(Derived), "x");
var prop = typeof(Base).GetProperty("Value");
var body = Expression.Property(param, prop);
var expr = Expression.Lambda<Func<Derived, int>>(body, param); instead of doing as i did var param = Expression.Parameter(typeof(Derived), "x");
var body = Expression.Property(param, "Value");
var expr = Expression.Lambda<Func<Derived, int>>(body, param); It's confusing. I think, this is inconsistency between lambda form and manually created one or am I wrong? |
Beta Was this translation helpful? Give feedback.
-
If you create it manually like that, what happens? |
Beta Was this translation helpful? Give feedback.
-
i expect to get the same result, but it's not the same |
Beta Was this translation helpful? Give feedback.
-
The same result as what? What two things do you expect to be equivalent? |
Beta Was this translation helpful? Give feedback.
-
I expect that x => x.Value and var param = Expression.Parameter(typeof(Derived), "x");
var body = Expression.Property(param, "Value");
var expr = Expression.Lambda<Func<Derived, int>>(body, param); but it's not what I exactly need. using System;
using System.Linq.Expressions;
public class RewriteAttribute : Attribute { }
public class Base { public virtual int Value { get; set; } }
public class Derived : Base {[Rewrite] public override int Value { get; set; } }
public class Rewriter : ExpressionVisitor
{
protected override Expression VisitMember(MemberExpression node)
{
//if (node.Member.IsDefined(typeof(RewriteAttribute), inherit: false)) <-- doesnt work
if (node.Expression.Type.GetProperty(node.Member.Name).IsDefined(typeof(RewriteAttribute), inherit: false))
{
return Rewrite(node);
}
return base.VisitMember(node);
}
private Expression Rewrite(MemberExpression node)
{
throw new NotImplementedException();
}
} instead of this |
Beta Was this translation helpful? Give feedback.
-
As mentioned, this is an incorrect expectation. This is not how the language works. You can look at the actual IL for just calling |
Beta Was this translation helpful? Give feedback.
-
Thank you for your patience. Now it's clear that my expectations are wrong. |
Beta Was this translation helpful? Give feedback.
-
Happy to help. We also have several community forums (where the team participates as well) to bounce ideas and questions off of. See: https://github.com/dotnet/roslyn#questions |
Beta Was this translation helpful? Give feedback.
-
Version Used: Version: 3.1.9 Commit: 774fc3d6a9
Steps to Reproduce:
Expected Behavior: returns true
Actual Behavior: returns false
PropertyBelongsToParameterIssue.zip
Beta Was this translation helpful? Give feedback.
All reactions