I'm building a C# expression-to-Javascript converter, along the lines of Linq-to-SQL, but I'm running into problems with compiler generated expression trees.
The particular problem I'm having is dealing with MemberExpression
values which were compiler generated, but which DO NOT have the CompilerGeneratedAttribute
specified on their types.
Here's a cut-down version of what I've been trying:
void ProcessMemberExpression(MemberExpression memberX) {
var expression = memberX.Expression;
var expressionType = expression.Type;
var customAttributes = expressionType.GetCustomAttributes(true);
var expressionTypeIsCompilerGenerated = customAttributes.Any(x => x is CompilerGeneratedAttribute);
if (expressionTypeIsCompilerGenerated) {
var memberExpressionValue = Expression.Lambda(memberX).Compile().DynamicInvoke();
... do stuff ...
}
else {
... do other stuff ...
}
}
Now, I have a Visual Studio debugging session open and I find this (running in the Immediate Window):
expressionType.Name
"<>c__DisplayClass64"
expressionType.GetCustomAttributes(true)
{object[0]}
expressionType.GetCustomAttributes(true).Length
0
So what I have here is an obviously compiler generated class with no custom attributes and hence no CompilerGeneratedAttribute
! Therefore, my code will do other stuff
, when I intend it to just do stuff
.
If anyone could help me out here, I'd be very grateful. If at all possible, I'd really rather not do anything sordid like matching the expressionType.Name
against something like <>.*__DisplayClass
.
Based on Jon Skeet's answer here, it sounds like checking for angle brackets will work.
Where/what is the private variable in auto-implemented property?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With