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
.
() => x + 1 - y
to be converted to the Javascript expressionX + 1 - Y
, where X and Y are the Javascript-side names for the C# names x and y, resp. Now, sometimes the C# compiler will hide that constant1
(well, this actually happened to me with a bool, but that's minor detail) with a generated class which I cannot inspect. Instead, I need to evaluate such terms and emit the JS for the resulting C# value, not the original expr. – Lizzielizzy