I want to list all downloaded assemblies in an Asp.NET application using AppDomain.CurrentDomain.GetAssemblies() . However, when checking the documentation for AppDomain, I find the following statement:
Thread safety
All public static (Shared in Visual Basic) members of this type are thread safe. Any instance members do not guarantee thread safety.
Since GetAssemblies() is an instance method, I take this because I need to do some kind of lock around this call, if not for something else, so that someone else cannot load the new assembly into the domain, m listing the current . I would expect AppDomain to provide some kind of SyncRoot property, but it is not, and I have not found any information on the Internet on how to do this.
How do I sync this call?
Edit
- I know that the lock statement is used to create a joint lock, for this reason I want to lock the AppDomain just like everyone else (or should), and not create my own lock that won’t prevent code that isn’t mine, from loading assemblies while I list them.
- I know that locks that can be accepted by anyone are usually bad ideas, but I also know that not doing locks when performing unsafe operations is even worse.
- Both answers still say that GetAssemblies () is essentially thread safe. It makes sense to me, and I really expect it to be, but how do you know that? Does anyone have a link to support this requirement? My google-fu didn't help me, and Reflector shows that this method is a thin shell around the internal inline method.
source share