Ejemplo n.º 1
0
        private Expression <Func <ActivityContext, TResult> > CompileLocationExpression(CodeActivityPublicEnvironmentAccessor publicAccessor, out string validationError)
        {
            Expression <Func <ActivityContext, TResult> > expressionTreeToReturn = null;

            validationError = null;
            try
            {
                expressionTreeToReturn = VisualBasicHelper.Compile <TResult>(this.ExpressionText, publicAccessor, true);
                // inspect the expressionTree to see if it is a valid location expression(L-value)
                string extraErrorMessage = null;
                if (!publicAccessor.ActivityMetadata.HasViolations && (expressionTreeToReturn == null || !ExpressionUtilities.IsLocation(expressionTreeToReturn, typeof(TResult), out extraErrorMessage)))
                {
                    string errorMessage = SR.InvalidLValueExpression;

                    if (extraErrorMessage != null)
                    {
                        errorMessage += ":" + extraErrorMessage;
                    }
                    expressionTreeToReturn = null;
                    validationError        = SR.CompilerErrorSpecificExpression(this.ExpressionText, errorMessage);
                }
            }
            catch (SourceExpressionException e)
            {
                validationError = e.Message;
            }

            return(expressionTreeToReturn);
        }
Ejemplo n.º 2
0
        public Expression GetExpressionTree()
        {
            if (this.IsMetadataCached)
            {
                if (this.expressionTree == null)
                {
                    // it's safe to create this CodeActivityMetadata here,
                    // because we know we are using it only as lookup purpose.
                    CodeActivityMetadata metadata = new CodeActivityMetadata(this, this.GetParentEnvironment(), false);
                    CodeActivityPublicEnvironmentAccessor publicAccessor = CodeActivityPublicEnvironmentAccessor.CreateWithoutArgument(metadata);
                    try
                    {
                        this.expressionTree = VisualBasicHelper.Compile <TResult>(this.ExpressionText, publicAccessor, false);
                    }
                    catch (SourceExpressionException e)
                    {
                        throw FxTrace.Exception.AsError(new InvalidOperationException(SR.VBExpressionTamperedSinceLastCompiled(e.Message)));
                    }
                    finally
                    {
                        metadata.Dispose();
                    }
                }

                Fx.Assert(this.expressionTree.NodeType == ExpressionType.Lambda, "Lambda expression required");
                return(ExpressionUtilities.RewriteNonCompiledExpressionTree((LambdaExpression)this.expressionTree));
            }
            else
            {
                throw FxTrace.Exception.AsError(new InvalidOperationException(SR.ActivityIsUncached));
            }
        }
        public static Expression <Func <ActivityContext, T> > Compile <T>(string expressionText, CodeActivityMetadata metadata)
        {
            HashSet <VisualBasicImportReference> allImportReferences = GetAllImportReferences(metadata.Environment.Root);
            VisualBasicHelper      helper                = new VisualBasicHelper(expressionText);
            HashSet <AssemblyName> refAssemNames         = new HashSet <AssemblyName>();
            HashSet <string>       namespaceImportsNames = new HashSet <string>();

            foreach (VisualBasicImportReference reference in allImportReferences)
            {
                if (reference.EarlyBoundAssembly != null)
                {
                    namespaceImportsNames.Add(reference.Import);
                    if (helper.referencedAssemblies == null)
                    {
                        helper.referencedAssemblies = new HashSet <Assembly>();
                    }
                    helper.referencedAssemblies.Add(reference.EarlyBoundAssembly);
                }
                else
                {
                    if (reference.AssemblyName != null)
                    {
                        refAssemNames.Add(reference.AssemblyName);
                    }
                    namespaceImportsNames.Add(reference.Import);
                }
            }
            helper.Initialize(refAssemNames, namespaceImportsNames);
            return(helper.Compile <T>(metadata));
        }
 protected override void CacheMetadata(CodeActivityMetadata metadata)
 {
     this.expressionTree = null;
     try
     {
         this.expressionTree = VisualBasicHelper.Compile <TResult>(this.ExpressionText, metadata);
     }
     catch (SourceExpressionException exception)
     {
         metadata.AddValidationError(exception.Message);
     }
 }
            public Type FindVariable(string name)
            {
                LocationReference reference = null;
                bool flag  = false;
                bool flag2 = false;

                for (LocationReferenceEnvironment environment = this.environmentProvider; (environment != null) && !flag2; environment = environment.Parent)
                {
                    foreach (LocationReference reference2 in environment.GetLocationReferences())
                    {
                        if (string.Equals(reference2.Name, name, StringComparison.OrdinalIgnoreCase))
                        {
                            if (flag)
                            {
                                flag2 = true;
                                break;
                            }
                            flag      = true;
                            reference = reference2;
                        }
                    }
                }
                if (flag2)
                {
                    this.errorMessage = System.Activities.SR.AmbiguousVBVariableReference(name);
                    return(null);
                }
                if (!flag)
                {
                    return(null);
                }
                Type           type           = reference.Type;
                HashSet <Type> typeReferences = null;

                VisualBasicHelper.EnsureTypeReferenced(type, ref typeReferences);
                foreach (Type type2 in typeReferences)
                {
                    if (!this.assemblies.Contains(type2.Assembly))
                    {
                        return(null);
                    }
                }
                return(type);
            }
Ejemplo n.º 6
0
        static void GetAllImportReferences(Activity activity, out List <string> namespaces, out List <string> assemblies)
        {
            List <AssemblyReference> referencedAssemblies;

            VisualBasicHelper.GetAllImportReferences(activity, true, out namespaces, out referencedAssemblies);

            assemblies = new List <string>();
            foreach (AssemblyReference reference in referencedAssemblies)
            {
                if (reference.AssemblyName != null)
                {
                    assemblies.Add(reference.AssemblyName.FullName);
                }
                else if (reference.Assembly != null)
                {
                    assemblies.Add(reference.Assembly.FullName);
                }
            }
        }
Ejemplo n.º 7
0
        protected override void CacheMetadata(CodeActivityMetadata metadata)
        {
            expressionTree = null;
            invoker        = new CompiledExpressionInvoker(this, false, metadata);
            if (metadata.Environment.CompileExpressions)
            {
                return;
            }
            // If ICER is not implemented that means we haven't been compiled
            var publicAccessor = CodeActivityPublicEnvironmentAccessor.Create(metadata);

            try
            {
                expressionTree = VisualBasicHelper.Compile <TResult>(this.ExpressionText, publicAccessor, false);
            }
            catch (SourceExpressionException e)
            {
                metadata.AddValidationError(e.Message);
            }
        }
Ejemplo n.º 8
0
        public static Activity RecompileVisualBasicValue(ActivityWithResult visualBasicValue, out Type returnType, out SourceExpressionException compileError, out VisualBasicSettings vbSettings)
        {
            IVisualBasicExpression expression = visualBasicValue as IVisualBasicExpression;

            if (expression == null)
            {
                throw FxTrace.Exception.AsError(new ArgumentException());
            }
            string expressionText = expression.ExpressionText;
            LocationReferenceEnvironment         parentEnvironment   = visualBasicValue.GetParentEnvironment();
            HashSet <VisualBasicImportReference> allImportReferences = VisualBasicHelper.GetAllImportReferences((parentEnvironment != null) ? parentEnvironment.Root : null);
            HashSet <string> namespaces           = new HashSet <string>();
            HashSet <string> referencedAssemblies = new HashSet <string>();

            foreach (VisualBasicImportReference reference in allImportReferences)
            {
                namespaces.Add(reference.Import);
                referencedAssemblies.Add(reference.Assembly);
            }
            return(CreatePrecompiledVisualBasicValue(null, expressionText, namespaces, referencedAssemblies, parentEnvironment, out returnType, out compileError, out vbSettings));
        }
Ejemplo n.º 9
0
 protected override void CacheMetadata(CodeActivityMetadata metadata)
 {
     this.expressionTree = null;
     try
     {
         this.expressionTree = VisualBasicHelper.Compile <TResult>(this.ExpressionText, metadata);
         string extraErrorMessage = null;
         if (!metadata.HasViolations && ((this.expressionTree == null) || !ExpressionUtilities.IsLocation(this.expressionTree, typeof(TResult), out extraErrorMessage)))
         {
             string invalidLValueExpression = System.Activities.SR.InvalidLValueExpression;
             if (extraErrorMessage != null)
             {
                 invalidLValueExpression = invalidLValueExpression + ":" + extraErrorMessage;
             }
             this.expressionTree = null;
             metadata.AddValidationError(System.Activities.SR.CompilerErrorSpecificExpression(this.ExpressionText, invalidLValueExpression));
         }
     }
     catch (SourceExpressionException exception)
     {
         metadata.AddValidationError(exception.Message);
     }
 }
Ejemplo n.º 10
0
        // create a pre-compiled VBValueExpression, and also provides expressin type back to the caller.
        //[SuppressMessage(FxCop.Category.Design, FxCop.Rule.AvoidOutParameters,
        //    Justification = "Design has been approved")]
        public static Activity CreatePrecompiledVisualBasicReference(Type targetType, string expressionText, IEnumerable <string> namespaces, IEnumerable <string> referencedAssemblies,
                                                                     LocationReferenceEnvironment environment,
                                                                     out Type returnType,
                                                                     out SourceExpressionException compileError,
                                                                     out VisualBasicSettings vbSettings)
        {
            LambdaExpression       lambda        = null;
            HashSet <string>       namespacesSet = new HashSet <string>();
            HashSet <AssemblyName> assembliesSet = new HashSet <AssemblyName>();

            compileError = null;
            returnType   = null;

            if (namespaces != null)
            {
                foreach (string ns in namespaces)
                {
                    if (ns != null)
                    {
                        namespacesSet.Add(ns);
                    }
                }
            }

            if (referencedAssemblies != null)
            {
                foreach (string assm in referencedAssemblies)
                {
                    if (assm != null)
                    {
                        assembliesSet.Add(new AssemblyName(assm));
                    }
                }
            }

            VisualBasicHelper vbhelper = new VisualBasicHelper(expressionText, assembliesSet, namespacesSet);

            if (targetType == null)
            {
                try
                {
                    lambda = vbhelper.CompileNonGeneric(environment);
                    if (lambda != null)
                    {
                        // inspect the expressionTree to see if it is a valid location expression(L-value)
                        string extraErrorMessage;
                        if (!ExpressionUtilities.IsLocation(lambda, targetType, out extraErrorMessage))
                        {
                            string errorMessage = SR.InvalidLValueExpression;
                            if (extraErrorMessage != null)
                            {
                                errorMessage += ":" + extraErrorMessage;
                            }
                            throw FxTrace.Exception.AsError(
                                      new SourceExpressionException(SR.CompilerErrorSpecificExpression(expressionText, errorMessage)));
                        }
                        returnType = lambda.ReturnType;
                    }
                }
                catch (SourceExpressionException e)
                {
                    compileError = e;
                    returnType   = typeof(object);
                }
                targetType = returnType;
            }
            else
            {
                MethodInfo genericCompileMethod = typeof(VisualBasicHelper).GetMethod("Compile", new Type[] { typeof(LocationReferenceEnvironment) });
                genericCompileMethod = genericCompileMethod.MakeGenericMethod(new Type[] { targetType });
                try
                {
                    lambda = (LambdaExpression)genericCompileMethod.Invoke(vbhelper, new object[] { environment });
                    // inspect the expressionTree to see if it is a valid location expression(L-value)
                    string extraErrorMessage = null;
                    if (!ExpressionUtilities.IsLocation(lambda, targetType, out extraErrorMessage))
                    {
                        string errorMessage = SR.InvalidLValueExpression;
                        if (extraErrorMessage != null)
                        {
                            errorMessage += ":" + extraErrorMessage;
                        }
                        throw FxTrace.Exception.AsError(
                                  new SourceExpressionException(SR.CompilerErrorSpecificExpression(expressionText, errorMessage)));
                    }
                    returnType = targetType;
                }
                catch (SourceExpressionException e)
                {
                    compileError = e;
                    returnType   = typeof(object);
                }
                catch (TargetInvocationException e)
                {
                    SourceExpressionException se = e.InnerException as SourceExpressionException;
                    if (se != null)
                    {
                        compileError = se;
                        returnType   = typeof(object);
                    }
                    else
                    {
                        throw FxTrace.Exception.AsError(e.InnerException);
                    }
                }
            }

            vbSettings = new VisualBasicSettings();
            if (lambda != null)
            {
                HashSet <Type> typeReferences = new HashSet <Type>();
                FindTypeReferences(lambda.Body, typeReferences);
                foreach (Type type in typeReferences)
                {
                    Assembly tassembly = type.Assembly;
                    if (tassembly.IsDynamic)
                    {
                        continue;
                    }
                    string assemblyName = VisualBasicHelper.GetFastAssemblyName(tassembly).Name;
                    VisualBasicImportReference importReference = new VisualBasicImportReference {
                        Assembly = assemblyName, Import = type.Namespace
                    };
                    vbSettings.ImportReferences.Add(importReference);
                }
            }

            Type concreteHelperType = VisualBasicExpressionFactoryType.MakeGenericType(targetType);
            VisualBasicExpressionFactory expressionFactory = (VisualBasicExpressionFactory)Activator.CreateInstance(concreteHelperType);

            return(expressionFactory.CreateVisualBasicReference(expressionText));
        }
Ejemplo n.º 11
0
        // create a pre-compiled VBValueExpression, and also provides expressin type back to the caller.
        //[SuppressMessage(FxCop.Category.Design, FxCop.Rule.AvoidOutParameters,
        //    Justification = "Design has been approved")]
        public static Activity CreatePrecompiledVisualBasicValue(Type targetType, string expressionText, IEnumerable <string> namespaces, IEnumerable <string> referencedAssemblies,
                                                                 LocationReferenceEnvironment environment,
                                                                 out Type returnType,
                                                                 out SourceExpressionException compileError,
                                                                 out VisualBasicSettings vbSettings)
        {
            LambdaExpression lambda = null;
            var namespacesSet       = new HashSet <string>();
            var assembliesSet       = new HashSet <AssemblyName>();

            compileError = null;
            returnType   = null;

            if (namespaces != null)
            {
                foreach (var ns in namespaces)
                {
                    if (ns != null)
                    {
                        namespacesSet.Add(ns);
                    }
                }
            }

            if (referencedAssemblies != null)
            {
                foreach (var assm in referencedAssemblies)
                {
                    if (assm != null)
                    {
                        assembliesSet.Add(new AssemblyName(assm));
                    }
                }
            }

            var vbhelper = new VisualBasicHelper(expressionText, assembliesSet, namespacesSet);

            if (targetType == null)
            {
                try
                {
                    lambda = vbhelper.CompileNonGeneric(environment);
                    if (lambda != null)
                    {
                        returnType = lambda.ReturnType;
                    }
                }
                catch (SourceExpressionException e)
                {
                    compileError = e;
                    returnType   = typeof(object);
                }
                targetType = returnType;
            }
            else
            {
                var genericCompileMethod = typeof(VisualBasicHelper).GetMethod("Compile", new Type[] { typeof(LocationReferenceEnvironment) });
                genericCompileMethod = genericCompileMethod.MakeGenericMethod(new Type[] { targetType });
                try
                {
                    lambda     = (LambdaExpression)genericCompileMethod.Invoke(vbhelper, new object[] { environment });
                    returnType = targetType;
                }
                catch (TargetInvocationException e)
                {
                    var se = e.InnerException as SourceExpressionException;
                    if (se != null)
                    {
                        compileError = se;
                        returnType   = typeof(object);
                    }
                    else
                    {
                        throw FxTrace.Exception.AsError(e.InnerException);
                    }
                }
            }

            vbSettings = new VisualBasicSettings();
            if (lambda != null)
            {
                var typeReferences = new HashSet <Type>();
                FindTypeReferences(lambda.Body, typeReferences);
                foreach (var type in typeReferences)
                {
                    var tassembly = type.Assembly;
                    if (tassembly.IsDynamic)
                    {
                        continue;
                    }
                    var assemblyName    = VisualBasicHelper.GetFastAssemblyName(tassembly).Name;
                    var importReference = new VisualBasicImportReference {
                        Assembly = assemblyName, Import = type.Namespace
                    };
                    vbSettings.ImportReferences.Add(importReference);
                }
            }

            var concreteHelperType = VisualBasicExpressionFactoryType.MakeGenericType(targetType);
            var expressionFactory  = (VisualBasicExpressionFactory)Activator.CreateInstance(concreteHelperType);

            return(expressionFactory.CreateVisualBasicValue(expressionText));
        }
Ejemplo n.º 12
0
        public static Activity CreatePrecompiledVisualBasicReference(Type targetType, string expressionText, IEnumerable <string> namespaces, IEnumerable <string> referencedAssemblies, LocationReferenceEnvironment environment, out Type returnType, out SourceExpressionException compileError, out VisualBasicSettings vbSettings)
        {
            LambdaExpression       expression            = null;
            HashSet <string>       namespaceImportsNames = new HashSet <string>();
            HashSet <AssemblyName> refAssemNames         = new HashSet <AssemblyName>();

            compileError = null;
            returnType   = null;
            if (namespaces != null)
            {
                foreach (string str in namespaces)
                {
                    if (str != null)
                    {
                        namespaceImportsNames.Add(str);
                    }
                }
            }
            if (referencedAssemblies != null)
            {
                foreach (string str2 in referencedAssemblies)
                {
                    if (str2 != null)
                    {
                        refAssemNames.Add(new AssemblyName(str2));
                    }
                }
            }
            VisualBasicHelper helper = new VisualBasicHelper(expressionText, refAssemNames, namespaceImportsNames);

            if (targetType == null)
            {
                try
                {
                    expression = helper.CompileNonGeneric(environment);
                    if (expression != null)
                    {
                        string str3;
                        if (!ExpressionUtilities.IsLocation(expression, targetType, out str3))
                        {
                            string invalidLValueExpression = System.Activities.SR.InvalidLValueExpression;
                            if (str3 != null)
                            {
                                invalidLValueExpression = invalidLValueExpression + ":" + str3;
                            }
                            throw FxTrace.Exception.AsError(new SourceExpressionException(System.Activities.SR.CompilerErrorSpecificExpression(expressionText, invalidLValueExpression)));
                        }
                        returnType = expression.ReturnType;
                    }
                }
                catch (SourceExpressionException exception)
                {
                    compileError = exception;
                    returnType   = typeof(object);
                }
                targetType = returnType;
            }
            else
            {
                MethodInfo info = typeof(VisualBasicHelper).GetMethod("Compile", new Type[] { typeof(LocationReferenceEnvironment) }).MakeGenericMethod(new Type[] { targetType });
                try
                {
                    expression = (LambdaExpression)info.Invoke(helper, new object[] { environment });
                    string extraErrorMessage = null;
                    if (!ExpressionUtilities.IsLocation(expression, targetType, out extraErrorMessage))
                    {
                        string str6 = System.Activities.SR.InvalidLValueExpression;
                        if (extraErrorMessage != null)
                        {
                            str6 = str6 + ":" + extraErrorMessage;
                        }
                        throw FxTrace.Exception.AsError(new SourceExpressionException(System.Activities.SR.CompilerErrorSpecificExpression(expressionText, str6)));
                    }
                    returnType = targetType;
                }
                catch (SourceExpressionException exception2)
                {
                    compileError = exception2;
                    returnType   = typeof(object);
                }
                catch (TargetInvocationException exception3)
                {
                    SourceExpressionException innerException = exception3.InnerException as SourceExpressionException;
                    if (innerException == null)
                    {
                        throw FxTrace.Exception.AsError(exception3.InnerException);
                    }
                    compileError = innerException;
                    returnType   = typeof(object);
                }
            }
            vbSettings = new VisualBasicSettings();
            if (expression != null)
            {
                HashSet <Type> typeReferences = new HashSet <Type>();
                FindTypeReferences(expression.Body, typeReferences);
                foreach (Type type in typeReferences)
                {
                    Assembly assembly = type.Assembly;
                    if (!assembly.IsDynamic)
                    {
                        string name = VisualBasicHelper.GetFastAssemblyName(assembly).Name;
                        VisualBasicImportReference item = new VisualBasicImportReference {
                            Assembly = name,
                            Import   = type.Namespace
                        };
                        vbSettings.ImportReferences.Add(item);
                    }
                }
            }
            VisualBasicExpressionFactory factory = (VisualBasicExpressionFactory)Activator.CreateInstance(VisualBasicExpressionFactoryType.MakeGenericType(new Type[] { targetType }));

            return(factory.CreateVisualBasicReference(expressionText));
        }