Example #1
0
        public Activity RecompileReference(ActivityWithResult lValue, out Type returnType, out SourceExpressionException compileError, out VisualBasicSettings vbSettings)
        {
            ITextExpression textExpression = lValue as ITextExpression;

            if (textExpression == null || textExpression.Language != Language)
            {
                throw FxTrace.Exception.AsError(new ArgumentException());
            }
            string expressionText = textExpression.ExpressionText;
            LocationReferenceEnvironment environment = lValue.GetParentEnvironment();

            List <string> namespaces;
            List <string> referencedAssemblies;

            GetAllImportReferences(lValue, out namespaces, out referencedAssemblies);

            return(CreatePrecompiledReference(
                       null,
                       expressionText,
                       namespaces,
                       referencedAssemblies,
                       environment,
                       out returnType,
                       out compileError,
                       out vbSettings));
        }
        public CompiledExpressionInvoker(ITextExpression expression, bool isReference, CodeActivityMetadata metadata)
        {
            if (expression == null)
            {
                throw FxTrace.Exception.ArgumentNull("expression");
            }

            if (metadata == null)
            {
                throw FxTrace.Exception.ArgumentNull("metadata");
            }

            this.expressionId = -1;
            this.textExpression = expression;
            this.expressionActivity = expression as Activity;
            this.isReference = isReference;
            this.locationReferences = new List<LocationReference>();
            this.metadata = metadata;
            this.accessor = CodeActivityPublicEnvironmentAccessor.Create(this.metadata);

            if (this.expressionActivity == null)
            {
                throw FxTrace.Exception.Argument("expression", SR.ITextExpressionParameterMustBeActivity);
            }

            ActivityWithResult resultActivity = this.expressionActivity as ActivityWithResult;

            this.metadataRoot = metadata.Environment.Root;

            this.ProcessLocationReferences();
        }
Example #3
0
        public CompiledExpressionInvoker(ITextExpression expression, bool isReference, CodeActivityMetadata metadata)
        {
            if (metadata == null)
            {
                throw FxTrace.Exception.ArgumentNull(nameof(metadata));
            }

            this.expressionId       = -1;
            this.textExpression     = expression ?? throw FxTrace.Exception.ArgumentNull(nameof(expression));
            this.expressionActivity = expression as Activity;
            this.isReference        = isReference;
            this.locationReferences = new List <LocationReference>();
            this.metadata           = metadata;
            this.accessor           = CodeActivityPublicEnvironmentAccessor.Create(this.metadata);

            if (this.expressionActivity == null)
            {
                throw FxTrace.Exception.Argument(nameof(expression), SR.ITextExpressionParameterMustBeActivity);
            }

            var resultActivity = this.expressionActivity as ActivityWithResult;

            this.metadataRoot = metadata.Environment.Root;

            this.ProcessLocationReferences();
        }
        // Recompile the VBReference passed in, with its current LocationReferenceEnvironment context
        // in a weakly-typed manner (the argument VBReference's type argument is ignored)
        //[SuppressMessage(FxCop.Category.Design, FxCop.Rule.AvoidOutParameters,
        //    Justification = "Design has been approved")]
        public static Activity RecompileVisualBasicReference(ActivityWithResult visualBasicReference,
                                                             out Type returnType,
                                                             out SourceExpressionException compileError,
                                                             out VisualBasicSettings vbSettings)
        {
            ITextExpression textExpression = visualBasicReference as ITextExpression;

            if (textExpression == null || textExpression.Language != VisualBasicHelper.Language)
            {
                // the argument must be of type VisualBasicReference<>
                throw FxTrace.Exception.AsError(new ArgumentException());
            }
            string expressionText = textExpression.ExpressionText;
            LocationReferenceEnvironment environment = visualBasicReference.GetParentEnvironment();

            IList <string> namespaces;
            IList <string> referencedAssemblies;

            GetAllImportReferences(visualBasicReference, out namespaces, out referencedAssemblies);

            return(CreatePrecompiledVisualBasicReference(
                       null,
                       expressionText,
                       namespaces,
                       referencedAssemblies,
                       environment,
                       out returnType,
                       out compileError,
                       out vbSettings));
        }
Example #5
0
        public CompiledLocation(Func <T> getMethod, Action <T> setMethod, IList <LocationReference> locationReferences, IList <Location> locations, int expressionId, Activity compiledRootActivity, ActivityContext currentActivityContext)
        {
            this.getMethod = getMethod;
            this.setMethod = setMethod;

            this.forImplementation  = currentActivityContext.Activity.MemberOf != currentActivityContext.Activity.RootActivity.MemberOf;
            this.locationReferences = locationReferences;
            this.locations          = locations;
            this.expressionId       = expressionId;

            this.compiledRootActivity = compiledRootActivity;
            this.expressionActivity   = currentActivityContext.Activity;
            //
            // Save the root activity instance to get the root activity post persistence
            // The root will always be alive as long as the location is valid, which is not
            // true for the activity instance of the expression that is executing
            this.rootInstance = currentActivityContext.CurrentInstance;
            while (this.rootInstance.Parent != null)
            {
                this.rootInstance = this.rootInstance.Parent;
            }
            //
            // Save the text of the expression for exception message
            ITextExpression textExpression = currentActivityContext.Activity as ITextExpression;

            if (textExpression != null)
            {
                this.expressionText = textExpression.ExpressionText;
            }
        }
Example #6
0
        public static string Generate(ITextExpression textExpr)
        {
            var composer = new TextExpressionComposer();

            composer.Visit(textExpr);

            return(composer.TextComposer.ToString());
        }
        internal static string GetExpressionString(Activity expression, ParserContext context)
        {
            string expressionString = null;

            if (expression != null)
            {
                Type expressionType         = expression.GetType();
                Type expressionArgumentType = expressionType.IsGenericType ? expressionType.GetGenericArguments()[0] : typeof(object);
                bool isLiteral = expressionType.IsGenericType ? Type.Equals(typeof(Literal <>), expressionType.GetGenericTypeDefinition()) : false;

                //handle ITextExpression
                if (expression is ITextExpression)
                {
                    ITextExpression textExpression = expression as ITextExpression;
                    expressionString = textExpression.ExpressionText;
                }
                //handle Literal Expression
                else if (isLiteral)
                {
                    TypeConverter converter = XamlUtilities.GetConverter(expressionArgumentType);
                    if (converter != null && converter.CanConvertTo(context, typeof(string)))
                    {
                        PropertyInfo literalValueProperty = expressionType.GetProperty("Value");
                        Fx.Assert(literalValueProperty != null && literalValueProperty.GetGetMethod() != null, "Literal<T> must have the Value property with a public get accessor.");
                        object literalValue    = literalValueProperty.GetValue(expression, null);
                        string convertedString = null;
                        if (literalValue != null)
                        {
                            try
                            {
                                convertedString = converter.ConvertToString(context, literalValue);
                            }
                            catch (ArgumentException)
                            {
                                convertedString = literalValue.ToString();
                            }
                        }
                        expressionString = expressionArgumentType == typeof(string) ? ("\"" + convertedString + "\"") : convertedString;
                    }
                }
                else if (expressionType.IsGenericType &&
                         (expressionType.GetGenericTypeDefinition() == typeof(VariableValue <>) ||
                          expressionType.GetGenericTypeDefinition() == typeof(VariableReference <>)))
                {
                    PropertyInfo variableProperty = expression.GetType().GetProperty("Variable");
                    Variable     variable         = variableProperty.GetValue(expression, null) as Variable;
                    if (variable != null)
                    {
                        expressionString = variable.Name;
                    }
                }
            }

            return(expressionString);
        }
Example #8
0
 public static string TryParseToTextExpression(this string textExpr, out ITextExpression expr)
 {
     try
     {
         expr = ParseToTextExpression(textExpr);
         return(string.Empty);
     }
     catch (Exception e)
     {
         expr = null;
         return(e.Message);
     }
 }
Example #9
0
        public virtual void Visit(ITextExpression textExpr)
        {
            if (ReferenceEquals(textExpr, null))
            {
                return;
            }

            var textExprNumber = textExpr as TeLiteralNumber;

            if (ReferenceEquals(textExprNumber, null) == false)
            {
                Visit(textExprNumber);
                return;
            }

            var textExprString = textExpr as TeLiteralString;

            if (ReferenceEquals(textExprString, null) == false)
            {
                Visit(textExprString);
                return;
            }

            var textExprIdentifier = textExpr as TeIdentifier;

            if (ReferenceEquals(textExprIdentifier, null) == false)
            {
                Visit(textExprIdentifier);
                return;
            }

            var textExprList = textExpr as TeList;

            if (ReferenceEquals(textExprList, null) == false)
            {
                Visit(textExprList);
                return;
            }

            var textExprDict = textExpr as TeDictionary;

            if (ReferenceEquals(textExprDict, null) == false)
            {
                Visit(textExprDict);
            }
        }
Example #10
0
            protected override void VisitITextExpression(Activity activity, out bool exit)
            {
                ITextExpression textExpression = activity as ITextExpression;

                if (textExpression != null)
                {
                    if (textExpression.RequiresCompilation)
                    {
                        this.RequiresCompilation = true;

                        if (this.languages == null)
                        {
                            this.languages = new HashSet <string>();
                        }

                        if (!this.languages.Contains(textExpression.Language))
                        {
                            this.languages.Add(textExpression.Language);
                        }
                    }
                }

                base.VisitITextExpression(activity, out exit);
            }
        internal static string ExpressionToString(object expression)
        {
            ITextExpression expr = expression as ITextExpression;

            return((expr != null) ? expr.ExpressionText : expression.ToString());
        }