private static void AttachNewInstance(Type compiledResultType, DynamicActivity dynamicActivity) { /* * https://docs.microsoft.com/en-us/dotnet/framework/windows-workflow-foundation/csharp-expressions */ // Create an instance of the new compiled expression type. ICompiledExpressionRoot compiledExpressionRoot = Activator .CreateInstance ( compiledResultType , new object[] { dynamicActivity } ) as ICompiledExpressionRoot; // Attach it to the activity. CompiledExpressionInvoker .SetCompiledExpressionRootForImplementation ( dynamicActivity , compiledExpressionRoot ); }
private bool CanExecuteExpression(ICompiledExpressionRoot compiledExpressionRoot, out int expressionId) { if (compiledExpressionRoot.CanExecuteExpression(this.textExpression.ExpressionText, this.isReference, locationReferences, out expressionId)) { return(true); } return(false); }
static void CreateCompiledActivity(DynamicActivity dynamicActivity, Type resultType) { ICompiledExpressionRoot compiledExpressionRoot = Activator.CreateInstance(resultType, new Object[] { dynamicActivity }) as ICompiledExpressionRoot; // Attach it to the activity. CompiledExpressionInvoker.SetCompiledExpressionRootForImplementation( dynamicActivity, compiledExpressionRoot); }
/// <summary> /// Compile the Expressions in workflow /// </summary> /// <param name="id">The workflow Id</param> /// <param name="activity">The workflow activity for build the expressions</param> static void CompileExpressions(string id, Activity activity) { Type t = GetType(id, activity); ICompiledExpressionRoot compiledExpressionRoot = Activator.CreateInstance(t, new object[] { activity }) as ICompiledExpressionRoot; CompiledExpressionInvoker.SetCompiledExpressionRootForImplementation( activity, compiledExpressionRoot); }
// Attached property setter for the compiled expression root for the implementation surface // area of an activity public static void SetCompiledExpressionRootForImplementation(object target, ICompiledExpressionRoot compiledExpressionRoot) { if (compiledExpressionRoot == null) { AttachablePropertyServices.RemoveProperty(target, compiledExpressionRootForImplementationProperty); } else { AttachablePropertyServices.SetProperty(target, compiledExpressionRootForImplementationProperty, compiledExpressionRoot); } }
private void RefreshAccessors() { // // If we've gotten here is means that we have a location that has roundtripped through persistence // CompiledDataContext & ICER don't round trip so we need to get them back from the current tree // and get new pointers to the get/set methods for this expression ICompiledExpressionRoot compiledRoot = GetCompiledExpressionRoot(); CompiledLocation <T> tempLocation = (CompiledLocation <T>)compiledRoot.InvokeExpression(this.expressionId, this.locations); this.getMethod = tempLocation.getMethod; this.setMethod = tempLocation.setMethod; }
static Type CompileExpressions(DynamicActivity dynamicActivity) { // activityName is the Namespace.Type of the activity that contains the // C# expressions. For Dynamic Activities this can be retrieved using the // name property , which must be in the form Namespace.Type. String activityName = dynamicActivity.Name; // Split activityName into Namespace and Type.Append _CompiledExpressionRoot to the type name // to represent the new type that represents the compiled expressions. // Take everything after the last . for the type name. String activityType = activityName.Split('.').Last() + "_CompiledExpressionRoot"; // Take everything before the last . for the namespace. String activityNamespace = String.Join(".", activityName.Split('.').Reverse().Skip(1).Reverse()); // Create a TextExpressionCompilerSettings. TextExpressionCompilerSettings settings = new TextExpressionCompilerSettings { Activity = dynamicActivity, Language = "C#", ActivityName = activityType, ActivityNamespace = activityNamespace, RootNamespace = null, GenerateAsPartialClass = false, AlwaysGenerateSource = true, ForImplementation = true }; // Compile the C# expression. TextExpressionCompilerResults results = new TextExpressionCompiler(settings).Compile(); // Any compilation errors are contained in the CompilerMessages. if (results.HasErrors) { throw new Exception("Compilation failed."); } // Create an instance of the new compiled expression type. ICompiledExpressionRoot compiledExpressionRoot = Activator.CreateInstance(results.ResultType, new Object[] { dynamicActivity }) as ICompiledExpressionRoot; // Attach it to the activity. CompiledExpressionInvoker.SetCompiledExpressionRootForImplementation( dynamicActivity, compiledExpressionRoot); return(results.ResultType); }
private static void Compile(IDynamicActivity dynamicActivity, LocationReferenceEnvironment environment) { #if NET45 string language = null; if (RequiresCompilation(dynamicActivity, environment, out language)) { TextExpressionCompiler compiler = new TextExpressionCompiler(GetCompilerSettings(dynamicActivity, language)); TextExpressionCompilerResults results = compiler.Compile(); if (results.HasErrors) { StringBuilder messages = new StringBuilder(); messages.Append("\r\n"); messages.Append("\r\n"); foreach (TextExpressionCompilerError message in results.CompilerMessages) { messages.Append("\t"); if (results.HasSourceInfo) { messages.Append(string.Concat(" ", SR.ActivityXamlServiceLineString, " ", message.SourceLineNumber, ": ")); } messages.Append(message.Message); } messages.Append("\r\n"); messages.Append("\r\n"); InvalidOperationException exception = new InvalidOperationException(SR.ActivityXamlServicesCompilationFailed(messages.ToString())); foreach (TextExpressionCompilerError message in results.CompilerMessages) { exception.Data.Add(message, message.Message); } throw FxTrace.Exception.AsError(exception); } Type compiledExpressionRootType = results.ResultType; ICompiledExpressionRoot compiledExpressionRoot = Activator.CreateInstance(compiledExpressionRootType, new object[] { dynamicActivity }) as ICompiledExpressionRoot; CompiledExpressionInvoker.SetCompiledExpressionRootForImplementation(dynamicActivity, compiledExpressionRoot); } #endif }
private static void Compile(IDynamicActivity dynamicActivity, ActivityXamlServicesSettings settings) { string language = null; if (!RequiresCompilation(dynamicActivity, settings.LocationReferenceEnvironment, out language)) { return; } var cSharpCompiler = settings.CSharpCompiler ?? new CSharpAheadOfTimeCompiler(); var compiler = new TextExpressionCompiler(GetCompilerSettings(dynamicActivity, language, cSharpCompiler)); var results = compiler.Compile(); if (results.HasErrors()) { var messages = new StringBuilder(); messages.Append("\r\n"); messages.Append("\r\n"); foreach (TextExpressionCompilerError message in results.CompilerMessages) { messages.Append("\t"); messages.Append(string.Concat(" ", SR.ActivityXamlServiceLineString, " ", message.SourceLineNumber, ": ")); messages.Append(message.Message); } messages.Append("\r\n"); messages.Append("\r\n"); InvalidOperationException exception = new InvalidOperationException(SR.ActivityXamlServicesCompilationFailed(messages.ToString())); foreach (TextExpressionCompilerError message in results.CompilerMessages) { exception.Data.Add(message, message.Message); } throw FxTrace.Exception.AsError(exception); } Type compiledExpressionRootType = results.ResultType; ICompiledExpressionRoot compiledExpressionRoot = Activator.CreateInstance(compiledExpressionRootType, new object[] { dynamicActivity }) as ICompiledExpressionRoot; CompiledExpressionInvoker.SetCompiledExpressionRootForImplementation(dynamicActivity, compiledExpressionRoot); }
static WorkflowService CreatetWorkflowServiceAndSetCompiledExpressionRoot(string supportedVersionXamlxfilePath, Stream activityStream, XName defaultServiceName) { WorkflowService service = CreatetWorkflowService(activityStream, defaultServiceName); if (service != null) { // CompiledExpression is not supported on Configuration Based Activation (CBA) scenario. if (ServiceHostingEnvironment.IsHosted && !ServiceHostingEnvironment.IsConfigurationBased) { // We use ServiceHostingEnvironment.FullVirtualPath (instead of the constructor string) because we’re passing this path to BuildManager, // which may not understand the file type referenced by the constructor string (e.g. .xaml). ICompiledExpressionRoot expressionRoot = XamlBuildProviderExtension.GetExpressionRoot(supportedVersionXamlxfilePath, service, ServiceHostingEnvironment.FullVirtualPath); if (expressionRoot != null) { CompiledExpressionInvoker.SetCompiledExpressionRoot(service.Body, expressionRoot); } } } return(service); }
private bool FindCompiledExpressionRoot(Activity activity, out ICompiledExpressionRoot compiledExpressionRoot) { if (CompiledExpressionInvoker.TryGetCompiledExpressionRoot(activity, this.forImplementation, out compiledExpressionRoot)) { if (compiledExpressionRoot.CanExecuteExpression(this.expressionText, true /* this is always a reference */, this.locationReferences, out this.expressionId)) { return(true); } } foreach (Activity containedActivity in WorkflowInspectionServices.GetActivities(activity)) { if (FindCompiledExpressionRoot(containedActivity, out compiledExpressionRoot)) { return(true); } } compiledExpressionRoot = null; return(false); }
private bool FindCompiledExpressionRoot(out int exprId, out ICompiledExpressionRoot compiledExpressionRoot) { var root = this.metadata.CurrentActivity.Parent; while (root != null) { if (CompiledExpressionInvoker.TryGetCompiledExpressionRoot(metadata.CurrentActivity, root, out var currentCompiledExpressionRoot)) { if (CanExecuteExpression(currentCompiledExpressionRoot, out exprId)) { compiledExpressionRoot = currentCompiledExpressionRoot; return(true); } } root = root.Parent; } exprId = -1; compiledExpressionRoot = null; return(false); }
private static void AttachNewInstance(Type compiledResultType, DynamicActivity dynamicActivity) { // Create an instance of the new compiled expression type. ICompiledExpressionRoot compiledExpressionRoot = Activator .CreateInstance ( compiledResultType , new object[] { dynamicActivity } ) as ICompiledExpressionRoot; // Attach it to the activity. CompiledExpressionInvoker .SetCompiledExpressionRootForImplementation ( dynamicActivity , compiledExpressionRoot ); }
bool TryGetCurrentCompiledExpressionRoot(ActivityContext activityContext, out ICompiledExpressionRoot compiledExpressionRoot, out int expressionId) { ActivityInstance current = activityContext.CurrentInstance; while (current != null && current.Activity != this.metadataRoot) { ICompiledExpressionRoot currentCompiledExpressionRoot = null; if (CompiledExpressionInvoker.TryGetCompiledExpressionRoot(current.Activity, true, out currentCompiledExpressionRoot)) { if (CanExecuteExpression(currentCompiledExpressionRoot, out expressionId)) { compiledExpressionRoot = currentCompiledExpressionRoot; return(true); } } current = current.Parent; } compiledExpressionRoot = null; expressionId = -1; return(false); }
// // Attached property setter for the compiled expression root for the public surface area of an activity public static void SetCompiledExpressionRoot(object target, ICompiledExpressionRoot compiledExpressionRoot) { if (compiledExpressionRoot == null) { AttachablePropertyServices.RemoveProperty(target, compiledExpressionRootProperty); } else { AttachablePropertyServices.SetProperty(target, compiledExpressionRootProperty, compiledExpressionRoot); } }
private bool TryGetCompiledExpressionRootAtDesignTime(Activity expression, Activity target, out ICompiledExpressionRoot compiledExpressionRoot, out int exprId) { exprId = -1; compiledExpressionRoot = null; if (!CompiledExpressionInvoker.TryGetCompiledExpressionRoot(expression, target, out compiledExpressionRoot) || !CanExecuteExpression(compiledExpressionRoot, out exprId)) { return(FindCompiledExpressionRoot(out exprId, out compiledExpressionRoot)); } return(true); }
// Helper to find the correct ICER for a given expression. This is separate from the above // because within this class we switch forImplementation for the same target Activity to // matched the ICER model of using one ICER for all expressions in the implementation and // root argument defaults. internal static bool TryGetCompiledExpressionRoot(Activity target, bool forImplementation, out ICompiledExpressionRoot compiledExpressionRoot) { if (!forImplementation) { compiledExpressionRoot = GetCompiledExpressionRoot(target) as ICompiledExpressionRoot; if (compiledExpressionRoot != null) { return(true); } // Default expressions for Arguments show up in the public surface area If we didn't // find an ICER for the public surface area continue and try to use the // implementation ICER } if (target is ICompiledExpressionRoot) { compiledExpressionRoot = (ICompiledExpressionRoot)target; return(true); } compiledExpressionRoot = GetCompiledExpressionRootForImplementation(target) as ICompiledExpressionRoot; if (compiledExpressionRoot != null) { return(true); } compiledExpressionRoot = null; return(false); }
// Internal helper to find the correct ICER for a given expression. internal static bool TryGetCompiledExpressionRoot(Activity expression, Activity target, out ICompiledExpressionRoot compiledExpressionRoot) { var forImplementation = expression.MemberOf != expression.RootActivity.MemberOf; return(TryGetCompiledExpressionRoot(target, forImplementation, out compiledExpressionRoot)); }
// // Internal helper to find the correct ICER for a given expression. internal static bool TryGetCompiledExpressionRoot(Activity expression, Activity target, out ICompiledExpressionRoot compiledExpressionRoot) { bool forImplementation = expression.MemberOf != expression.RootActivity.MemberOf; return TryGetCompiledExpressionRoot(target, forImplementation, out compiledExpressionRoot); }
// // Helper to find the correct ICER for a given expression. // This is separate from the above because within this class we switch forImplementation for the same target Activity // to matched the ICER model of using one ICER for all expressions in the implementation and root argument defaults. internal static bool TryGetCompiledExpressionRoot(Activity target, bool forImplementation, out ICompiledExpressionRoot compiledExpressionRoot) { if (!forImplementation) { compiledExpressionRoot = GetCompiledExpressionRoot(target) as ICompiledExpressionRoot; if (compiledExpressionRoot != null) { return true; } // // Default expressions for Arguments show up in the public surface area // If we didn't find an ICER for the public surface area continue // and try to use the implementation ICER } if (target is ICompiledExpressionRoot) { compiledExpressionRoot = (ICompiledExpressionRoot)target; return true; } compiledExpressionRoot = GetCompiledExpressionRootForImplementation(target) as ICompiledExpressionRoot; if (compiledExpressionRoot != null) { return true; } compiledExpressionRoot = null; return false; }
bool TryGetCurrentCompiledExpressionRoot(ActivityContext activityContext, out ICompiledExpressionRoot compiledExpressionRoot, out int expressionId) { ActivityInstance current = activityContext.CurrentInstance; while (current != null && current.Activity != this.metadataRoot) { ICompiledExpressionRoot currentCompiledExpressionRoot = null; if (CompiledExpressionInvoker.TryGetCompiledExpressionRoot(current.Activity, true, out currentCompiledExpressionRoot)) { if (CanExecuteExpression(currentCompiledExpressionRoot, out expressionId)) { compiledExpressionRoot = currentCompiledExpressionRoot; return true; } } current = current.Parent; } compiledExpressionRoot = null; expressionId = -1; return false; }
bool FindCompiledExpressionRoot(out int exprId, out ICompiledExpressionRoot compiledExpressionRoot) { Activity root = this.metadata.CurrentActivity.Parent; while (root != null) { ICompiledExpressionRoot currentCompiledExpressionRoot = null; if (CompiledExpressionInvoker.TryGetCompiledExpressionRoot(metadata.CurrentActivity, root, out currentCompiledExpressionRoot)) { if (CanExecuteExpression(currentCompiledExpressionRoot, out exprId)) { compiledExpressionRoot = currentCompiledExpressionRoot; return true; } } root = root.Parent; } exprId = -1; compiledExpressionRoot = null; return false; }
bool TryGetCompiledExpressionRootAtDesignTime(Activity expression, Activity target, out ICompiledExpressionRoot compiledExpressionRoot, out int exprId) { exprId = -1; compiledExpressionRoot = null; if (!CompiledExpressionInvoker.TryGetCompiledExpressionRoot(expression, target, out compiledExpressionRoot) || !CanExecuteExpression(compiledExpressionRoot, out exprId)) { return FindCompiledExpressionRoot(out exprId, out compiledExpressionRoot); } return true; }
bool CanExecuteExpression(ICompiledExpressionRoot compiledExpressionRoot, out int expressionId) { if (compiledExpressionRoot.CanExecuteExpression(this.textExpression.ExpressionText, this.isReference, locationReferences, out expressionId)) { return true; } return false; }