Beispiel #1
0
        private Type ResolveType(out string errorTitle, out string errorMessage)
        {
            errorTitle   = null;
            errorMessage = null;
            Type result = this.SelectedType;

            try
            {
                IMultiTargetingSupportService multiTargetingSupport = this.Context.Services.GetService <IMultiTargetingSupportService>();
                if (multiTargetingSupport != null)
                {
                    result = multiTargetingSupport.GetRuntimeType(result);
                }

                if (result == null)
                {
                    errorTitle   = SR.TypeBrowserErrorMessageTitle;
                    errorMessage = SR.TypeBrowserError;
                    return(null);
                }

                if (result.IsGenericTypeDefinition)
                {
                    bool isValid = true;
                    //get number of generic parameters in edited type
                    Type[] arguments = new Type[this.GenericTypeMapping.Count];

                    //for each argument, get resolved type
                    for (int i = 0; i < this.GenericTypeMapping.Count && isValid; ++i)
                    {
                        arguments[i] = this.GenericTypeMapping[i].GetConcreteType();
                        if (multiTargetingSupport != null && arguments[i] != null)
                        {
                            arguments[i] = multiTargetingSupport.GetRuntimeType(arguments[i]);
                        }
                        isValid = isValid && (null != arguments[i]);
                    }

                    //if all parameters are resolved, create concrete type
                    if (isValid)
                    {
                        result = result.MakeGenericType(arguments);
                    }
                    else
                    {
                        errorTitle   = SR.TypeBrowserErrorMessageTitle;
                        errorMessage = SR.TypeResolverError;
                        result       = null;
                    }
                }
            }
            catch (ArgumentException err)
            {
                errorTitle   = err.GetType().Name;
                errorMessage = err.Message;
                return(null);
            }

            return(result);
        }
        public static Assembly GetAssembly(AssemblyName assemblyName, IMultiTargetingSupportService multiTargetingService)
        {
            Assembly assembly = null;

            try
            {
                if (multiTargetingService != null)
                {
                    assembly = multiTargetingService.GetReflectionAssembly(assemblyName);
                }
                else
                {
                    assembly = Assembly.Load(assemblyName);
                }
            }
            catch (FileNotFoundException)
            {
                //this exception may occur if current project is not compiled yet
            }
            catch (FileLoadException)
            {
                //the assembly could not be loaded, ignore the error
            }
            catch (BadImageFormatException)
            {
                //bad assembly
            }
            return(assembly);
        }
Beispiel #3
0
        void GetAvailableNamespaces()
        {
            Fx.Assert(this.availableNamespaces != null, "available namespace table should have been set before calling this method");
            AssemblyContextControlItem assemblyItem = this.Context.Items.GetValue <AssemblyContextControlItem>();

            if (assemblyItem != null)
            {
                IMultiTargetingSupportService multiTargetingService = this.Context.Services.GetService <IMultiTargetingSupportService>();

                ////When ReferencedAssemblyNames is null, it's in rehost scenario. And we need to preload assemblies in
                ////TextExpression.ReferencesForImplementation/References if there is any. So that these assemblies will be returned
                ////by AssemblyContextControlItem.GetEnvironmentAssemblies and user can see namespaces defined in these assemlbies
                ////in the dropdown list of Import Designer.
                if ((assemblyItem.ReferencedAssemblyNames == null) && (this.targetFramework.Is45OrHigher()))
                {
                    ModelTreeManager          modelTreeManager = this.Context.Services.GetService <ModelTreeManager>();
                    object                    root             = modelTreeManager.Root.GetCurrentValue();
                    IList <AssemblyReference> references;
                    NamespaceHelper.GetTextExpressionNamespaces(root, out references);
                    foreach (AssemblyReference reference in references)
                    {
                        reference.LoadAssembly();
                    }
                }

                IEnumerable <Assembly> allAssemblies = assemblyItem.GetEnvironmentAssemblies(multiTargetingService);
                if (assemblyItem.LocalAssemblyName != null)
                {
                    allAssemblies = allAssemblies.Union <Assembly>(new Collection <Assembly> {
                        AssemblyContextControlItem.GetAssembly(assemblyItem.LocalAssemblyName, multiTargetingService)
                    });
                }

                foreach (Assembly assembly in allAssemblies)
                {
                    try
                    {
                        if (assembly != null)
                        {
                            Fx.Assert(!assembly.IsDynamic, "there should not be any dynamic assemblies in reference list");
                            this.UpdateAvailableNamespaces(assembly, assemblyItem);
                        }
                    }
                    catch (ReflectionTypeLoadException)
                    {
                    }
                    catch (FileNotFoundException)
                    {
                    }
                }

                if (assemblyItem.LocalAssemblyName == null)
                {
                    AppDomain.CurrentDomain.AssemblyLoad += this.proxy.OnAssemblyLoad;
                }
            }
        }
Beispiel #4
0
 internal void OnReferenceUpdated(AssemblyName updatedReference, bool isAdded)
 {
     if (this.availableNamespaces != null)
     {
         IMultiTargetingSupportService multiTargetingService = this.Context.Services.GetService <IMultiTargetingSupportService>();
         Assembly assembly = AssemblyContextControlItem.GetAssembly(updatedReference, multiTargetingService);
         Fx.Assert(assembly != null, "Assembly shouldn't be null here.");
         // In normal case, assembly shouldn't be null. In case there's any situation we're overlooked, we should ignore instead of throwing exceptions.
         if (assembly != null)
         {
             OnReferenceUpdated(assembly, isAdded);
         }
     }
 }
 public IEnumerable<Assembly> GetEnvironmentAssemblies (IMultiTargetingSupportService multiTargetingService)
 {
     if (this.ReferencedAssemblyNames == null)
     {
         return AppDomain.CurrentDomain.GetAssemblies().Where<Assembly>(assembly => !assembly.IsDynamic);
     }
     else
     {
         List<Assembly> assemblies = new List<Assembly>();
         foreach (AssemblyName assemblyName in this.ReferencedAssemblyNames)
         {
             Assembly assembly = GetAssembly(assemblyName, multiTargetingService);
             if (assembly != null)
             {
                 assemblies.Add(assembly);
             }
         }
         return assemblies;
     }
 }
 public IEnumerable <Assembly> GetEnvironmentAssemblies(IMultiTargetingSupportService multiTargetingService)
 {
     if (this.ReferencedAssemblyNames == null)
     {
         return(AppDomain.CurrentDomain.GetAssemblies().Where <Assembly>(assembly => !assembly.IsDynamic));
     }
     else
     {
         List <Assembly> assemblies = new List <Assembly>();
         foreach (AssemblyName assemblyName in this.ReferencedAssemblyNames)
         {
             Assembly assembly = GetAssembly(assemblyName, multiTargetingService);
             if (assembly != null)
             {
                 assemblies.Add(assembly);
             }
         }
         return(assemblies);
     }
 }
 public static Assembly GetAssembly(AssemblyName assemblyName, IMultiTargetingSupportService multiTargetingService)
 {
     Assembly assembly = null;
     try
     {
         if (multiTargetingService != null)
         {
             assembly = multiTargetingService.GetReflectionAssembly(assemblyName);
         }
         else
         {
             assembly = Assembly.Load(assemblyName);
         }
     }  
     catch (FileNotFoundException)
     {
         //this exception may occur if current project is not compiled yet
     }
     catch (FileLoadException)
     {
         //the assembly could not be loaded, ignore the error
     }
     catch (BadImageFormatException)
     {
         //bad assembly
     }
     return assembly;
 }
Beispiel #8
0
        internal static void ConvertToVBSettings(IList <string> importedNamespaces, IList <AssemblyReference> references, EditingContext context, out VisualBasicSettings settings)
        {
            Dictionary <string, List <string> > visualBasicImports = new Dictionary <string, List <string> >();

            foreach (string importedNamespace in importedNamespaces)
            {
                visualBasicImports.Add(importedNamespace, new List <string>());
            }

            Collection <Assembly>         assemblies            = new Collection <Assembly>();
            IMultiTargetingSupportService multiTargetingService = context.Services.GetService <IMultiTargetingSupportService>();

            foreach (AssemblyReference reference in references)
            {
                Assembly assembly;
                if (multiTargetingService == null)
                {
                    reference.LoadAssembly();
                    assembly = reference.Assembly;
                }
                else
                {
                    assembly = AssemblyContextControlItem.GetAssembly(reference.AssemblyName, multiTargetingService);
                }

                if (assembly != null)
                {
                    assemblies.Add(assembly);
                }
            }

            AssemblyContextControlItem assemblyContextItem = context.Items.GetValue <AssemblyContextControlItem>();
            AssemblyName localAssembly = null;

            if (assemblyContextItem != null)
            {
                localAssembly = assemblyContextItem.LocalAssemblyName;
            }

            if (localAssembly != null)
            {
                Assembly assembly = AssemblyContextControlItem.GetAssembly(localAssembly, multiTargetingService);
                if (assembly != null)
                {
                    assemblies.Add(assembly);
                }
            }

            foreach (Assembly assembly in assemblies)
            {
                foreach (Type type in assembly.GetTypes())
                {
                    string ns = type.Namespace;
                    if ((ns != null) && visualBasicImports.ContainsKey(ns))
                    {
                        string assemblyName = assembly.GetName().Name;
                        visualBasicImports[ns].Add(assemblyName);
                    }
                }
            }

            settings = new VisualBasicSettings();
            foreach (KeyValuePair <string, List <string> > entries in visualBasicImports)
            {
                string importedNamespace = entries.Key;
                foreach (string assemblyName in entries.Value)
                {
                    settings.ImportReferences.Add(new VisualBasicImportReference
                    {
                        Import   = importedNamespace,
                        Assembly = assemblyName
                    });
                }
            }
        }