public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            AssemblyReference reference = value as AssemblyReference;

            if (destinationType == typeof(string) && reference != null)
            {
                if (reference.AssemblyName != null)
                {
                    return(reference.AssemblyName.ToString());
                }
                else if (reference.Assembly != null)
                {
                    XamlSchemaContext schemaContext = GetSchemaContext(context);
                    if (schemaContext == null || schemaContext.FullyQualifyAssemblyNamesInClrNamespaces)
                    {
                        return(reference.Assembly.FullName);
                    }
                    else
                    {
                        AssemblyName assemblyName = AssemblyReference.GetFastAssemblyName(reference.Assembly);
                        return(assemblyName.Name);
                    }
                }
                else
                {
                    return(null);
                }
            }

            return(base.ConvertTo(context, culture, value, destinationType));
        }
        private static Assembly ResolveAssembly(AssemblyName assemblyReference, IEnumerable <Assembly> assemblies)
        {
            foreach (Assembly assembly in assemblies)
            {
                AssemblyName assemblyName = AssemblyReference.GetFastAssemblyName(assembly);
                if (AssemblyReference.AssemblySatisfiesReference(assemblyName, assemblyReference))
                {
                    return(assembly);
                }
            }

            return(null);
        }
Exemple #3
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));
        }
Exemple #4
0
        public static VisualBasicSettings CollectXmlNamespacesAndAssemblies(ITypeDescriptorContext context)
        {
            // access XamlSchemaContext.ReferenceAssemblies
            // for the Compiled Xaml scenario
            IList <Assembly>           xsCtxReferenceAssemblies  = null;
            IXamlSchemaContextProvider xamlSchemaContextProvider = context.GetService(typeof(IXamlSchemaContextProvider)) as IXamlSchemaContextProvider;

            if (xamlSchemaContextProvider != null && xamlSchemaContextProvider.SchemaContext != null)
            {
                xsCtxReferenceAssemblies = xamlSchemaContextProvider.SchemaContext.ReferenceAssemblies;
                if (xsCtxReferenceAssemblies != null && xsCtxReferenceAssemblies.Count == 0)
                {
                    xsCtxReferenceAssemblies = null;
                }
            }

            VisualBasicSettings    settings          = null;
            IXamlNamespaceResolver namespaceResolver = (IXamlNamespaceResolver)context.GetService(typeof(IXamlNamespaceResolver));

            if (namespaceResolver == null)
            {
                return(null);
            }

            lock (AssemblyCache.XmlnsMappingsLockObject)
            {
                // Fetch xmlnsMappings for the prefixes returned by the namespaceResolver service

                foreach (NamespaceDeclaration prefix in namespaceResolver.GetNamespacePrefixes())
                {
                    ReadOnlyXmlnsMapping mapping;
                    WrapCachedMapping(prefix, out mapping);
                    if (!mapping.IsEmpty)
                    {
                        if (settings == null)
                        {
                            settings = new VisualBasicSettings();
                        }

                        if (!mapping.IsEmpty)
                        {
                            foreach (ReadOnlyVisualBasicImportReference importReference in mapping.ImportReferences)
                            {
                                if (xsCtxReferenceAssemblies != null)
                                {
                                    // this is "compiled Xaml"
                                    VisualBasicImportReference newImportReference;

                                    if (importReference.EarlyBoundAssembly != null)
                                    {
                                        if (xsCtxReferenceAssemblies.Contains(importReference.EarlyBoundAssembly))
                                        {
                                            newImportReference = importReference.Clone();
                                            newImportReference.EarlyBoundAssembly = importReference.EarlyBoundAssembly;
                                            settings.ImportReferences.Add(newImportReference);
                                        }
                                        continue;
                                    }

                                    for (int i = 0; i < xsCtxReferenceAssemblies.Count; i++)
                                    {
                                        AssemblyName xsCtxAssemblyName = AssemblyReference.GetFastAssemblyName(xsCtxReferenceAssemblies[i]);
                                        if (importReference.AssemblySatisfiesReference(xsCtxAssemblyName))
                                        {
                                            // bind this assembly early to the importReference
                                            // so later AssemblyName resolution can be skipped
                                            newImportReference = importReference.Clone();
                                            newImportReference.EarlyBoundAssembly = xsCtxReferenceAssemblies[i];
                                            settings.ImportReferences.Add(newImportReference);
                                            break;
                                        }
                                    }
                                }
                                else
                                {
                                    // this is "loose Xaml"
                                    VisualBasicImportReference newImportReference = importReference.Clone();
                                    if (importReference.EarlyBoundAssembly != null)
                                    {
                                        // VBImportReference.Clone() method deliberately doesn't copy
                                        // its EarlyBoundAssembly to the cloned instance.
                                        // we need to explicitly copy the original's EarlyBoundAssembly
                                        newImportReference.EarlyBoundAssembly = importReference.EarlyBoundAssembly;
                                    }
                                    settings.ImportReferences.Add(newImportReference);
                                }
                            }
                        }
                    }
                }
            }
            return(settings);
        }