C# is there a way to validate assemblies loaded?

Viewed 235

Is there a way in .NET to validate loaded assemblies and if needed prevent to load it?

I was trying to use AsseblyLoad event, but it seems to be swallowing exception according to this:

static void Main(string[] args)
{
    AppDomain.CurrentDomain.AssemblyLoad += OnAssemblyLoaded;
    var asm = Assembly.LoadFrom(@"path\to\assembly.dll");

    Console.WriteLine($"loaded {asm.GetName().Name}");            
    Console.ReadKey();
}

private static void OnAssemblyLoaded(object sender, AssemblyLoadEventArgs args)
{
    // DO CHECKs here            
    throw new Exception("invalid assebly");
}

In real project, I want to verify publisher due to licence rules. I'm missing the place where I can put my custom validation code before loading assembly.

1 Answers
Related