< Summary

Information
Class: Ice.Internal.AssemblyUtil
Assembly: Ice
File(s): /home/runner/work/ice/ice/csharp/src/Ice/Internal/AssemblyUtil.cs
Tag: 71_18251537082
Line coverage
84%
Covered lines: 32
Uncovered lines: 6
Coverable lines: 38
Total lines: 101
Line coverage: 84.2%
Branch coverage
100%
Covered branches: 14
Total branches: 14
Branch coverage: 100%
Method coverage
100%
Covered methods: 5
Total methods: 5
Method coverage: 100%

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%11100%
createInstance(...)100%1.13150%
preloadAssemblies()100%11100%
loadAssemblies()100%1010100%
loadReferencedAssemblies(...)100%4.37471.43%

File(s)

/home/runner/work/ice/ice/csharp/src/Ice/Internal/AssemblyUtil.cs

#LineLine coverage
 1// Copyright (c) ZeroC, Inc.
 2
 3using System.Collections;
 4using System.Reflection;
 5using System.Runtime.InteropServices;
 6
 7namespace Ice.Internal;
 8
 9public static class AssemblyUtil
 10{
 111    public static readonly bool isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
 112    public static readonly bool isMacOS = RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
 113    public static readonly bool isLinux = RuntimeInformation.IsOSPlatform(OSPlatform.Linux);
 14
 15    public static object createInstance(Type t)
 16    {
 17        try
 18        {
 119            return System.Activator.CreateInstance(t);
 20        }
 021        catch (MemberAccessException)
 22        {
 023            return null;
 24        }
 125    }
 26
 27    public static void preloadAssemblies()
 28    {
 129        lock (_mutex)
 30        {
 131            loadAssemblies(); // Lazy initialization
 132        }
 133    }
 34
 35    //
 36    // Make sure that all assemblies that are referenced by this process
 37    // are actually loaded. This is necessary so we can use reflection
 38    // on any type in any assembly because the type we are after will
 39    // most likely not be in the current assembly and, worse, may be
 40    // in an assembly that has not been loaded yet. (Type.GetType()
 41    // is no good because it looks only in the calling object's assembly
 42    // and mscorlib.dll.)
 43    //
 44    private static void loadAssemblies()
 45    {
 146        Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
 147        List<Assembly> newAssemblies = null;
 148        foreach (Assembly a in assemblies)
 49        {
 150            if (!_loadedAssemblies.Contains(a.FullName))
 51            {
 152                newAssemblies ??= new List<Assembly>();
 153                newAssemblies.Add(a);
 154                _loadedAssemblies[a.FullName] = a;
 55            }
 56        }
 157        if (newAssemblies != null)
 58        {
 159            foreach (Assembly a in newAssemblies)
 60            {
 161                loadReferencedAssemblies(a);
 62            }
 63        }
 164    }
 65
 66    private static void loadReferencedAssemblies(Assembly a)
 67    {
 68        try
 69        {
 170            AssemblyName[] names = a.GetReferencedAssemblies();
 171            foreach (AssemblyName name in names)
 72            {
 173                if (!_loadedAssemblies.ContainsKey(name.FullName))
 74                {
 75                    try
 76                    {
 177                        var ra = Assembly.Load(name);
 78                        //
 79                        // The value of name.FullName may not match that of ra.FullName, so
 80                        // we record the assembly using both keys.
 81                        //
 182                        _loadedAssemblies[name.FullName] = ra;
 183                        _loadedAssemblies[ra.FullName] = ra;
 184                        loadReferencedAssemblies(ra);
 185                    }
 086                    catch (System.Exception)
 87                    {
 88                        // Ignore assemblies that cannot be loaded.
 089                    }
 90                }
 91            }
 192        }
 093        catch (PlatformNotSupportedException)
 94        {
 95            // Some platforms like UWP do not support using GetReferencedAssemblies
 096        }
 197    }
 98
 199    private static readonly Hashtable _loadedAssemblies = []; // <string, Assembly> pairs.
 1100    private static readonly object _mutex = new();
 101}