Exemple #1
0
 internal AssemblyLoader(TypeProvider typeProvider, string filePath)
 {
     this.isLocalAssembly = false;
     this.typeProvider    = typeProvider;
     if (!File.Exists(filePath))
     {
         throw new FileNotFoundException();
     }
     System.Reflection.AssemblyName assemblyName = System.Reflection.AssemblyName.GetAssemblyName(filePath);
     if (assemblyName != null)
     {
         ITypeResolutionService service = (ITypeResolutionService)typeProvider.GetService(typeof(ITypeResolutionService));
         if (service != null)
         {
             try
             {
                 this.assembly = service.GetAssembly(assemblyName);
                 if ((((this.assembly == null) && (assemblyName.GetPublicKeyToken() != null)) && ((assemblyName.GetPublicKeyToken().GetLength(0) == 0) && (assemblyName.GetPublicKey() != null))) && (assemblyName.GetPublicKey().GetLength(0) == 0))
                 {
                     System.Reflection.AssemblyName name = (System.Reflection.AssemblyName)assemblyName.Clone();
                     name.SetPublicKey(null);
                     name.SetPublicKeyToken(null);
                     this.assembly = service.GetAssembly(name);
                 }
             }
             catch
             {
             }
         }
         if (this.assembly == null)
         {
             try
             {
                 if (MultiTargetingInfo.MultiTargetingUtilities.IsFrameworkReferenceAssembly(filePath))
                 {
                     this.assembly = System.Reflection.Assembly.Load(assemblyName.FullName);
                 }
                 else
                 {
                     this.assembly = System.Reflection.Assembly.Load(assemblyName);
                 }
             }
             catch
             {
             }
         }
     }
     if (this.assembly == null)
     {
         this.assembly = System.Reflection.Assembly.LoadFrom(filePath);
     }
 }
Exemple #2
0
            /// <summary>
            /// <para>Gets hold of the DTE Reference object and from there, opens the assembly of the
            /// ActiveX control we want to create. It then walks through all AxHost derived classes
            /// in that assembly, and returns the type that matches our control's CLSID.</para>
            /// </summary>
            private Type GetAxTypeFromReference(object reference, IDesignerHost host)
            {
                string path = (string)reference.GetType().InvokeMember("Path", BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance, null, reference, null, CultureInfo.InvariantCulture);

                // Missing reference will show up as an empty string.
                //
                if (path == null || path.Length <= 0)
                {
                    return(null);
                }

                FileInfo file     = new FileInfo(path);
                string   fullPath = file.FullName;

                Debug.WriteLineIf(AxToolSwitch.TraceVerbose, "Checking: " + fullPath);

                ITypeResolutionService trs = (ITypeResolutionService)host.GetService(typeof(ITypeResolutionService));

                Debug.Assert(trs != null, "No type resolution service found.");

                Assembly a = trs.GetAssembly(AssemblyName.GetAssemblyName(fullPath));

                Debug.Assert(a != null, "No assembly found at " + fullPath);

                return(GetAxTypeFromAssembly(a));
            }
Exemple #3
0
 private Assembly OnAssemblyResolve(object sender, ResolveEventArgs args)
 {
     AppDomain.CurrentDomain.AssemblyResolve -= new ResolveEventHandler(OnAssemblyResolve);
     try
     {
         return(resolutionService.GetAssembly(ReflectionHelper.ParseAssemblyName(args.Name), false));
     }
     finally
     {
         AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(OnAssemblyResolve);
     }
 }
Exemple #4
0
        protected virtual Type GetType(IDesignerHost host, AssemblyName assemblyName, string typeName, bool reference)
        {
            if (typeName == null)
            {
                throw new ArgumentNullException("typeName");
            }

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

            //get ITypeResolutionService from host, as we have no other IServiceProvider here
            ITypeResolutionService typeRes = host.GetService(typeof(ITypeResolutionService)) as ITypeResolutionService;
            Type type = null;

            if (typeRes != null)
            {
                //TODO: Using Assembly loader to throw errors. Silent fail and return null?
                typeRes.GetAssembly(assemblyName, true);
                if (reference)
                {
                    typeRes.ReferenceAssembly(assemblyName);
                }
                type = typeRes.GetType(typeName, true);
            }
            else
            {
                Assembly assembly = Assembly.Load(assemblyName);
                if (assembly != null)
                {
                    type = assembly.GetType(typeName);
                }
            }
            return(type);
        }
        /// <summary>
        /// See <see cref="ITypeResolutionService.GetAssembly(AssemblyName, bool)"/>.
        /// </summary>
        public virtual Assembly GetAssembly(AssemblyName name, bool throwOnError)
        {
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }
            if (name.Name.Length == 0)
            {
                throw new ArgumentException(Properties.Resources.General_ArgumentEmpty, "name");
            }

            // Method 1: stored in our cache.
            Assembly asm = (Assembly)assemblyCache[name.FullName];

            if (asm != null)
            {
                return(asm);
            }

            try
            {
                // Method 2: Assembly.Load, regular CLR probing.
                asm = Assembly.Load(name);
            }
            catch (FileNotFoundException)
            {
                // Method 3: Assembly.LoadFrom relative to our base path.
                string asmpath = Path.Combine(basePath, name.Name + ".dll");
                if (File.Exists(asmpath))
                {
                    // We know the file exists. Dependencies will automatically be resolved
                    // relative to the LoadFrom location.
                    // See http://blogs.msdn.com/suzcook/archive/2003/05/29/57143.aspx
                    asm = Assembly.LoadFrom(asmpath);
                }
                // Method 2b: Assembly.LoadWithPartialName.
                if (asm == null)
                {
                    try
                    {
                        asm = Assembly.LoadWithPartialName(name.Name);
                    }
                    catch (BadImageFormatException)
                    {
                    }
                }
            }

            if (asm == null && parentService != null)
            {
                // Method 5, ask parent service. At this point we can let it throw, as there's
                // no need to specialize the returned exception message further.
                asm = parentService.GetAssembly(name, throwOnError);
            }

            if (asm == null && throwOnError)
            {
                throw new FileNotFoundException(String.Format(
                                                    System.Globalization.CultureInfo.CurrentCulture,
                                                    Properties.Resources.TypeResolutionService_AssemblyNotFound,
                                                    name.FullName));
            }

            // Cache it for faster retrieval next time.
            if (asm != null)
            {
                //Cache using the assemblyName we received to the method.
                if (!assemblyCache.ContainsKey(name.FullName))
                {
                    assemblyCache.Add(name.FullName, asm);
                }
            }

            return(asm);
        }
        protected virtual Type GetType(IDesignerHost host, AssemblyName assemblyName, string typeName, bool reference)
        {
            ITypeResolutionService ts = null;
            Type type = null;

            if (typeName == null)
            {
                throw new ArgumentNullException("typeName");
            }

            if (host != null)
            {
                ts = (ITypeResolutionService)host.GetService(typeof(ITypeResolutionService));
            }

            if (ts != null)
            {
                if (reference)
                {
                    if (assemblyName != null)
                    {
                        ts.ReferenceAssembly(assemblyName);
                        type = ts.GetType(typeName);
                    }
                    else
                    {
                        // Just try loading the type.  If we succeed, then use this as the
                        // reference.
                        type = ts.GetType(typeName);
                        if (type == null)
                        {
                            type = Type.GetType(typeName);
                        }
                        if (type != null)
                        {
                            ts.ReferenceAssembly(type.Assembly.GetName());
                        }
                    }
                }
                else
                {
                    if (assemblyName != null)
                    {
                        Assembly a = ts.GetAssembly(assemblyName);
                        if (a != null)
                        {
                            type = a.GetType(typeName);
                        }
                    }

                    if (type == null)
                    {
                        type = ts.GetType(typeName);
                    }
                }
            }
            else
            {
                if (!String.IsNullOrEmpty(typeName))
                {
                    if (assemblyName != null)
                    {
                        Assembly a = null;
                        try {
                            a = Assembly.Load(assemblyName);
                        }
                        catch (FileNotFoundException) {
                        }
                        catch (BadImageFormatException) {
                        }
                        catch (IOException) {
                        }

                        if (a == null && assemblyName.CodeBase != null && assemblyName.CodeBase.Length > 0)
                        {
                            try {
                                a = Assembly.LoadFrom(assemblyName.CodeBase);
                            }
                            catch (FileNotFoundException) {
                            }
                            catch (BadImageFormatException) {
                            }
                            catch (IOException) {
                            }
                        }

                        if (a != null)
                        {
                            type = a.GetType(typeName);
                        }
                    }

                    if (type == null)
                    {
                        type = Type.GetType(typeName, false);
                    }
                }
            }

            return(type);
        }
Exemple #7
0
        protected virtual Type GetType(IDesignerHost host, System.Reflection.AssemblyName assemblyName, string typeName, bool reference)
        {
            ITypeResolutionService service = null;
            Type type = null;

            if (typeName == null)
            {
                throw new ArgumentNullException("typeName");
            }
            if (host != null)
            {
                service = (ITypeResolutionService)host.GetService(typeof(ITypeResolutionService));
            }
            if (service != null)
            {
                if (reference)
                {
                    if (assemblyName != null)
                    {
                        service.ReferenceAssembly(assemblyName);
                        return(service.GetType(typeName));
                    }
                    type = service.GetType(typeName);
                    if (type == null)
                    {
                        type = Type.GetType(typeName);
                    }
                    if (type != null)
                    {
                        service.ReferenceAssembly(type.Assembly.GetName());
                    }
                    return(type);
                }
                if (assemblyName != null)
                {
                    Assembly assembly = service.GetAssembly(assemblyName);
                    if (assembly != null)
                    {
                        type = assembly.GetType(typeName);
                    }
                }
                if (type == null)
                {
                    type = service.GetType(typeName);
                }
                return(type);
            }
            if (!string.IsNullOrEmpty(typeName))
            {
                if (assemblyName != null)
                {
                    Assembly assembly2 = null;
                    try
                    {
                        assembly2 = Assembly.Load(assemblyName);
                    }
                    catch (FileNotFoundException)
                    {
                    }
                    catch (BadImageFormatException)
                    {
                    }
                    catch (IOException)
                    {
                    }
                    if (((assembly2 == null) && (assemblyName.CodeBase != null)) && (assemblyName.CodeBase.Length > 0))
                    {
                        try
                        {
                            assembly2 = Assembly.LoadFrom(assemblyName.CodeBase);
                        }
                        catch (FileNotFoundException)
                        {
                        }
                        catch (BadImageFormatException)
                        {
                        }
                        catch (IOException)
                        {
                        }
                    }
                    if (assembly2 != null)
                    {
                        type = assembly2.GetType(typeName);
                    }
                }
                if (type == null)
                {
                    type = Type.GetType(typeName, false);
                }
            }
            return(type);
        }
Exemple #8
0
        internal AssemblyLoader(TypeProvider typeProvider, string filePath)
        {
            this.isLocalAssembly = false;
            this.typeProvider    = typeProvider;
            if (File.Exists(filePath))
            {
                AssemblyName asmName = AssemblyName.GetAssemblyName(filePath);
                if (asmName != null)
                {
                    // Try loading the assembly using type resolution service first.
                    ITypeResolutionService trs = (ITypeResolutionService)typeProvider.GetService(typeof(ITypeResolutionService));
                    if (trs != null)
                    {
                        try
                        {
                            this.assembly = trs.GetAssembly(asmName);
                            //

                            if (this.assembly == null && asmName.GetPublicKeyToken() != null && (asmName.GetPublicKeyToken().GetLength(0) == 0) && asmName.GetPublicKey() != null && (asmName.GetPublicKey().GetLength(0) == 0))
                            {
                                AssemblyName partialName = (AssemblyName)asmName.Clone();
                                partialName.SetPublicKey(null);
                                partialName.SetPublicKeyToken(null);
                                this.assembly = trs.GetAssembly(partialName);
                            }
                        }
                        catch
                        {
                            // Eat up any exceptions!
                        }
                    }

                    // If type resolution service wasn't available or it failed use Assembly.Load
                    if (this.assembly == null)
                    {
                        try
                        {
                            if (MultiTargetingInfo.MultiTargetingUtilities.IsFrameworkReferenceAssembly(filePath))
                            {
                                this.assembly = Assembly.Load(asmName.FullName);
                            }
                            else
                            {
                                this.assembly = Assembly.Load(asmName);
                            }
                        }
                        catch
                        {
                            // Eat up any exceptions!
                        }
                    }
                }
                // If Assembly.Load also failed, use Assembly.LoadFrom
                if (this.assembly == null)
                {
                    this.assembly = Assembly.LoadFrom(filePath);
                }
            }
            else
            {
                // TypeProvider will handle this and report the error
                throw new FileNotFoundException();
            }
        }
        // InvokeMethods the currently selected method, if any, when
        // the InvokeMethod button is pressed.
        private void InvokeMethod(object sender, EventArgs e)
        {
            // If the GetAssembly or GetPathofAssembly radio button
            // is selected.
            if (this.radioButton1.Checked || this.radioButton2.Checked || this.radioButton4.Checked)
            {
                if (this.entryBox.Text.Length == 0)
                {
                    // If there is no assembly name specified, display status message.
                    this.infoBox.Text = "You must enter the name of the assembly to retrieve.";
                }
                else if (rs != null)
                {
                    // Create a System.Reflection.AssemblyName
                    // for the entered text.
                    AssemblyName name = new AssemblyName();
                    name.Name = this.entryBox.Text.Trim();

                    // If the GetAssembly radio button is checked...
                    if (this.radioButton1.Checked)
                    {
                        // Use the ITypeResolutionService to attempt to
                        // resolve an assembly reference.
                        Assembly a = rs.GetAssembly(name, false);
                        // If an assembly matching the specified name was not
                        // located in the GAC or local project references,
                        // display a message.
                        if (a == null)
                        {
                            this.infoBox.Text = "The " + this.entryBox.Text + " assembly could not be located.";
                        }
                        else
                        {
                            // An assembly matching the specified name was located.
                            // Builds a list of types.
                            Type[]        types = a.GetTypes();
                            StringBuilder sb    = new StringBuilder();
                            for (int i = 0; i < types.Length; i++)
                            {
                                sb.Append(types[i].FullName + "\r\n");
                            }
                            string path = rs.GetPathOfAssembly(name);
                            // Displays assembly information and a list of types contained in the assembly.
                            this.infoBox.Text = "Assembly located:\r\n\r\n" + a.FullName + "\r\n  at: " + path + "\r\n\r\nAssembly types:\r\n\r\n" + sb.ToString();
                        }
                    }
                    else if (this.radioButton2.Checked)
                    {
                        string path = rs.GetPathOfAssembly(name);
                        if (path != null)
                        {
                            this.infoBox.Text = "Assembly located at:\r\n" + path;
                        }
                        else
                        {
                            this.infoBox.Text = "Assembly was not located.";
                        }
                    }
                    else if (this.radioButton4.Checked)
                    {
                        Assembly a = null;
                        try
                        {
                            // Add a reference to the assembly to the
                            // current project.
                            rs.ReferenceAssembly(name);
                            // Use the ITypeResolutionService to attempt
                            // to resolve an assembly reference.
                            a = rs.GetAssembly(name, false);
                        }
                        catch
                        {
                            // Catch this exception so that the exception
                            // does not interrupt control behavior.
                        }
                        // If an assembly matching the specified name was not
                        // located in the GAC or local project references,
                        // display a message.
                        if (a == null)
                        {
                            this.infoBox.Text = "The " + this.entryBox.Text + " assembly could not be located.";
                        }
                        else
                        {
                            this.infoBox.Text = "A reference to the " + a.FullName + " assembly has been added to the project's referenced assemblies.";
                        }
                    }
                }
            }
            else if (this.radioButton3.Checked)
            {
                if (this.entryBox.Text.Length == 0)
                {
                    // If there is no type name specified, display a
                    // status message.
                    this.infoBox.Text = "You must enter the name of the type to retrieve.";
                }
                else if (rs != null)
                {
                    Type type = null;
                    try
                    {
                        type = rs.GetType(this.entryBox.Text, false, true);
                    }
                    catch
                    {
                        // Consume exceptions raised during GetType call
                    }
                    if (type != null)
                    {
                        // Display type information.
                        this.infoBox.Text = "Type: " + type.FullName + " located.\r\n  Namespace: " + type.Namespace + "\r\n" + type.AssemblyQualifiedName;
                    }
                    else
                    {
                        this.infoBox.Text = "Type not located.";
                    }
                }
            }
        }
 /// <summary>
 /// See <see cref="ITypeResolutionService.GetAssembly(AssemblyName, bool)"/>.
 /// </summary>
 public override Assembly GetAssembly(AssemblyName name, bool throwOnError)
 {
     return(parentLoader.GetAssembly(name, throwOnError));
 }