public override LambdaExpression CompileExpression(ExpressionToCompile expressionToCompile) { var options = ScriptOptions.Default .AddReferences(expressionToCompile.ReferencedAssemblies.GetMetadataReferences()) .AddImports(expressionToCompile.ImportedNamespaces); options = AddOptions(options); var untypedExpressionScript = VisualBasicScript.Create($"? {expressionToCompile.Code}", options); var compilation = untypedExpressionScript.GetCompilation(); var syntaxTree = compilation.SyntaxTrees.First(); var identifiers = IdentifiersWalker.GetIdentifiers(compilation, syntaxTree); var resolvedIdentifiers = identifiers .Select(name => (Name: name, Type: expressionToCompile.VariableTypeGetter(name))) .Where(var => var.Type != null) .ToArray(); const string Comma = ", "; var names = string.Join(Comma, resolvedIdentifiers.Select(var => var.Name)); var types = string.Join(Comma, resolvedIdentifiers .Select(var => var.Type) .Concat(new[] { expressionToCompile.LambdaReturnType }) .Select(VisualBasicObjectFormatter.FormatTypeName)); var finalCompilation = compilation.ReplaceSyntaxTree(syntaxTree, syntaxTree.WithChangedText(SourceText.From( $"Public Shared Function CreateExpression() As Expression(Of Func(Of {types}))\nReturn Function({names}) ({expressionToCompile.Code})\nEnd Function"))); var results = ScriptingAheadOfTimeCompiler.BuildAssembly(finalCompilation); if (results.HasErrors) { throw FxTrace.Exception.AsError(new SourceExpressionException(SR.CompilerErrorSpecificExpression(expressionToCompile.Code, results), results.CompilerMessages)); } return((LambdaExpression)results.ResultType.GetMethod("CreateExpression").Invoke(null, null)); }
public override LambdaExpression CompileExpression(ExpressionToCompile expressionToCompile) { var options = ScriptOptions.Default .AddReferences(expressionToCompile.ReferencedAssemblies) .AddImports(expressionToCompile.ImportedNamespaces); var untypedExpressionScript = VisualBasicScript.Create($"? {expressionToCompile.ExpressionString}", options); var identifiers = IdentifiersWalker.GetIdentifiers(untypedExpressionScript); var resolvedIdentifiers = identifiers .Select(name => (Name: name, Type: expressionToCompile.VariableTypeGetter(name))) .Where(var => var.Type != null) .ToArray(); const string Comma = ", "; var names = string.Join(Comma, resolvedIdentifiers.Select(var => var.Name)); var types = string.Join(Comma, resolvedIdentifiers .Select(var => var.Type) .Concat(new[] { expressionToCompile.LambdaReturnType ?? typeof(object) }) .Select(VisualBasicObjectFormatter.FormatTypeName)); var typedExpressionScript = VisualBasicScript .Create($"Dim resultExpression As Expression(Of Func(Of {types})) = Function({names}) ({expressionToCompile.ExpressionString})", options) .ContinueWith("? resultExpression", options); try { return((LambdaExpression)typedExpressionScript.RunAsync().GetResult().ReturnValue); } catch (CompilationErrorException ex) { throw FxTrace.Exception.AsError(new SourceExpressionException(SR.CompilerErrorSpecificExpression(expressionToCompile.ExpressionString, ex.ToString()))); } }
public LambdaExpression CompileExpression(string expressionString, Func <string, Type> getVariableType, ScriptOptions options, Type lambdaReturnType = null) { var untypedExpressionScript = VisualBasicScript.Create($"? {expressionString}", options); var identifiers = IdentifiersWalker.GetIdentifiers(untypedExpressionScript); var resolvedIdentifiers = identifiers .Select(name => (Name: name, Type: getVariableType(name))) .Where(var => var.Type != null) .ToArray(); const string Comma = ", "; var names = string.Join(Comma, resolvedIdentifiers.Select(var => var.Name)); var types = string.Join(Comma, resolvedIdentifiers .Select(var => var.Type) .Concat(new[] { lambdaReturnType ?? typeof(object) }) .Select(type => GetTypeName(type))); var typedExpressionScript = VisualBasicScript .Create($"Dim resultExpression As Expression(Of Func(Of {types})) = Function({names}) ({expressionString})", options) .ContinueWith("? resultExpression", options); return((LambdaExpression)typedExpressionScript.RunAsync().GetAwaiter().GetResult().ReturnValue); }