// 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();

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

            GetAllImportReferences(visualBasicReference, out namespaces, out referencedAssemblies);

            return(CreatePrecompiledVisualBasicReference(
                       null,
                       expressionText,
                       namespaces,
                       referencedAssemblies,
                       environment,
                       out returnType,
                       out compileError,
                       out vbSettings));
        }
 public static Activity CreatePrecompiledReference(Type targetType, string expressionText, Activity parent, out Type returnType, out SourceExpressionException compileError) =>
 Impl.CreatePrecompiledReference(targetType, expressionText, parent, out returnType, out compileError);
 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) =>
 Impl.CreatePrecompiledReference(targetType, expressionText, namespaces, referencedAssemblies, environment, out returnType, out compileError, out vbSettings);
 // 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) =>
 Impl.RecompileReference(visualBasicReference, out returnType, out compileError, out vbSettings);
示例#5
0
        public Activity CreatePrecompiledValue(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));
                    }
                }
            }

            var compilerHelper = CreateJitCompilerHelper(expressionText, assembliesSet, namespacesSet);

            if (targetType == null)
            {
                try
                {
                    lambda = compilerHelper.CompileNonGeneric(environment);
                    if (lambda != null)
                    {
                        returnType = lambda.ReturnType;
                    }
                }
                catch (SourceExpressionException e)
                {
                    compileError = e;
                    returnType   = typeof(object);
                }
                targetType = returnType;
            }
            else
            {
                MethodInfo genericCompileMethod = compilerHelper.GetType().GetMethod("Compile", new Type[] { typeof(LocationReferenceEnvironment) });
                genericCompileMethod = genericCompileMethod.MakeGenericMethod(new Type[] { targetType });
                try
                {
                    lambda     = (LambdaExpression)genericCompileMethod.Invoke(compilerHelper, new object[] { environment });
                    returnType = targetType;
                }
                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 = AssemblyReference.GetFastAssemblyName(tassembly).Name;
                    VisualBasicImportReference importReference = new VisualBasicImportReference {
                        Assembly = assemblyName, Import = type.Namespace
                    };
                    vbSettings.ImportReferences.Add(importReference);
                }
            }

            Type concreteHelperType             = ExpressionFactoryType.MakeGenericType(targetType);
            ExpressionFactory expressionFactory = (ExpressionFactory)Activator.CreateInstance(concreteHelperType);

            return(expressionFactory.CreateValue(expressionText));
        }
示例#6
0
 internal Activity CreatePrecompiledValue(Type targetType, string expressionText, Activity parent, out Type returnType, out SourceExpressionException compileError)
 {
     GetAllImportReferences(parent, out var namespaces, out var assemblies);
     return(CreatePrecompiledValue(targetType, expressionText, namespaces, assemblies, parent.PublicEnvironment, out returnType, out compileError, out _));
 }
        // 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));
        }
示例#8
0
 private static void Check(string text, Type resultType, Activity value, Type returnType, SourceExpressionException compileError)
 {
     ((ITextExpression)value).ExpressionText.ShouldBe(text);
     ((ActivityWithResult)value).ResultType.ShouldBe(resultType);
     returnType.ShouldBe(resultType);
     compileError.ShouldBeNull();
 }
示例#9
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));
        }
示例#10
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));
        }