开发者

Is there a way to run code when an assembly loads?

开发者 https://www.devze.com 2023-02-20 18:36 出处:网络
I\'m building an application that will load untrusted assemblies for inspection (i.e. retrieval of the assembly full name).For security reasons, I\'m trying to think of a way that these assemblies cou

I'm building an application that will load untrusted assemblies for inspection (i.e. retrieval of the assembly full name). For security reasons, I'm trying to think of a way that these assemblies could be written that would allow them to execute code when loaded. I haven't code up with a method yet, but wanted to throw it out here to see if anyone could.

I'm aware that I could load these assemblies into an untrusted app domain, effectively sto开发者_运维百科pping them from doing almost anything, but I wanted to avoid the complexity if it's un-needed.

Specifically, I will be calling Assembly.Load and <LoadedAssebmly>.FullName. Maybe there's a better way to load the assembly name without using the Assembly class?

Thanks, Matt


First of all, there's the AssemblyName class. It allows you to find the assembly's name without loading it. Second, you can load assemblies using Assembly.ReflectionOnlyLoad, which uses the reflection-only context -- no code can be executed from such an assembly.


Yes, it is possible: .Net: Running code when assembly is loaded

I suggest, you use a method to inspect the assembly, that doesn't load it, i.e. Mono.Cecil

0

精彩评论

暂无评论...
验证码 换一张
取 消