none
Custom assemblies by GetReferencedAssemblies RRS feed

  • Question

  • I need to get the referenced assemblies of a particular assembly, but not the .net framework assemblies. I'd like to get the "custom" assemblies.

    For example, when I call GetReferencedAssemblies() on a particular assembly, here's the list I get:

    I don't know why there are repeated ones, but I would like to get only the Progress.04glrt, is that possible?

    I'm asking that because I will loop through all "custom" referenced assemblies and also call GetReferencedAssemblies on them in order to get all the "custom" referenced assemblies. I don't want to call GetReferencedAssemblies() on a framework assembly.

    By "custom" I mean all assemblies written by anyone except Microsoft.


    Take a look at WPF FlashMessage

    Wednesday, September 19, 2012 6:28 PM

Answers

  • There's nothing in the AssemblyName (or Assembly) class that tells you if an assembly is "custom" or not. Some alternatives:

    • use a list of known .NET assembly names: mscorlib, all System.*, all Microsoft.* etc.
    • search for the AssemblyCompany attribute on the assembly and check if it contains Microsoft, this requires the assembly to be loaded which may be a bit of a problem
    • check the Location/CodeBase of the assembly to see if it's in the .NET install dir
    • check the public key token of the assembly - Microsoft uses more than one so it may be problematic to keep track of all of them
    • Marked as answer by Joba Diniz Thursday, September 20, 2012 12:16 PM
    Thursday, September 20, 2012 5:09 AM
    Moderator

All replies

  • There's nothing in the AssemblyName (or Assembly) class that tells you if an assembly is "custom" or not. Some alternatives:

    • use a list of known .NET assembly names: mscorlib, all System.*, all Microsoft.* etc.
    • search for the AssemblyCompany attribute on the assembly and check if it contains Microsoft, this requires the assembly to be loaded which may be a bit of a problem
    • check the Location/CodeBase of the assembly to see if it's in the .NET install dir
    • check the public key token of the assembly - Microsoft uses more than one so it may be problematic to keep track of all of them
    • Marked as answer by Joba Diniz Thursday, September 20, 2012 12:16 PM
    Thursday, September 20, 2012 5:09 AM
    Moderator
  • Use the below function which gives you all non framework assemblies.

    public static List<AssemblyName> GetReferencedAssemblies()
    {
        List<AssemblyName> result = new List<AssemblyName>();
    
        var assemblies = Assembly.GetExecutingAssembly().GetReferencedAssemblies();
        foreach (var item in assemblies)
        {
            Assembly curAsm = Assembly.(item.FullName);
            var attribs = curAsm.GetCustomAttributes(typeof(AssemblyCompanyAttribute), true);
            if (attribs.Length > 0)
            {
                string company = ((AssemblyCompanyAttribute)attribs[0]).Company;
                if (company == "Microsoft Corporation")
                {
                    continue;
                }
            }
    
            result.Add(item);
        }
    
        return result;
    }

    I hope this helps.

    Please mark this post as answer if it solved your problem. Happy Programming!

    Thursday, September 20, 2012 5:13 AM
  • Thank you guys...

    I came up with this solution:

    private IEnumerable<Assembly> ObterDependencias(Assembly biblioteca)
            {
                foreach (var nomeBiblioteca in biblioteca.GetReferencedAssemblies())
                {
                    var bibliotecaDependencia = Assembly.ReflectionOnlyLoad(nomeBiblioteca.FullName);
                    if (!BibliotecaDaMicrosoft(bibliotecaDependencia))
                    {
                        yield return bibliotecaDependencia;
    
                        foreach (var dependencia in ObterDependencias(bibliotecaDependencia))
                            yield return dependencia;
                    }
                }
            }
    
            private bool BibliotecaDaMicrosoft(Assembly biblioteca)
            {
                var dadoAtributo = biblioteca.GetCustomAttributesData().FirstOrDefault(c => c.ToString().Contains("AssemblyCompanyAttribute"));
                if (dadoAtributo != null)
                {
                    var empresa = dadoAtributo.ConstructorArguments.First().Value.ToString();
                    return empresa.StartsWith("Microsoft");
                }
    
                return false;
            }


    Take a look at WPF FlashMessage

    Thursday, September 20, 2012 12:15 PM