Image.IsAssembly C# (CSharp) Method

IsAssembly() public static method

public static IsAssembly ( string file ) : bool
file string
return bool
    public static bool IsAssembly(string file)
    {
        using (var stream = File.OpenRead(file))
        {
            if (stream.Length < 318)
            {
                return false;
            }
            if (stream.ReadUInt16() != 0x5a4d)
            {
                return false;
            }
            if (!stream.Advance(58))
            {
                return false;
            }
            if (!stream.MoveTo(stream.ReadUInt32()))
            {
                return false;
            }
            if (stream.ReadUInt32() != 0x00004550)
            {
                return false;
            }
            if (!stream.Advance(20))
            {
                return false;
            }
            if (!stream.Advance(stream.ReadUInt16() == 0x20b ? 222 : 206))
            {
                return false;
            }

            return stream.ReadUInt32() != 0;
        }
    }

Usage Example

Example #1
0
    public void FindInterceptor()
    {
        LogDebug("Searching for an intercepter");

        var interceptor = types.FirstOrDefault(x => x.IsInterceptor());

        if (interceptor != null)
        {
            var logMethod = interceptor.Methods.FirstOrDefault(x => x.Name == "Log");
            if (logMethod == null)
            {
                throw new WeavingException($"Could not find 'Log' method on '{interceptor.FullName}'.");
            }
            VerifyHasCorrectParameters(logMethod);
            VerifyMethodIsPublicStatic(logMethod);
            LogMethod = logMethod;
            return;
        }

        foreach (var referencePath in ReferenceCopyLocalPaths)
        {
            if (!referencePath.EndsWith(".dll") && !referencePath.EndsWith(".exe"))
            {
                continue;
            }

            var stopwatch = Stopwatch.StartNew();

            if (!Image.IsAssembly(referencePath))
            {
                LogDebug($"Skipped checking '{referencePath}' since it is not a .net assembly.");
                continue;
            }
            LogDebug($"Reading module from '{referencePath}'");
            var moduleDefinition = ReadModule(referencePath);

            stopwatch.Stop();

            interceptor = moduleDefinition
                          .GetTypes()
                          .FirstOrDefault(x => x.IsInterceptor());
            if (interceptor == null)
            {
                continue;
            }
            if (!interceptor.IsPublic)
            {
                LogInfo($"Did not use '{interceptor.FullName}' since it is not public.");
                continue;
            }
            var logMethod = interceptor.Methods.FirstOrDefault(x => x.Name == "Log");
            if (logMethod == null)
            {
                throw new WeavingException($"Could not find 'Log' method on '{interceptor.FullName}'.");
            }
            VerifyHasCorrectParameters(logMethod);
            VerifyMethodIsPublicStatic(logMethod);
            LogMethod = ModuleDefinition.ImportReference(logMethod);
            return;
        }
    }
All Usage Examples Of Image::IsAssembly