简体   繁体   中英

How to resolve CA 1009 warning in Visual Studio

I have some C# code that loads an assembly from a certain path like so:

assembly = System.Reflection.Assembly.LoadFrom(assemblyPath);

When I compile, Visual Studio Code Analysis throws this warning :

warning : CA2001 : Microsoft.Reliability : Remove the call to Assembly.LoadFrom ...

I'm looking for a way to resolve this warning (without suppressing it). Is there an alternative to LoadFrom that'll do that for me (I know LoadFile isn't it )?

If your code works and does what you expect , then overrule the warning. The warning is there to bring your attention to things that might have edge-cases. If you have read what it says about CA 1009, and you understand the caveats, then: move on.

For loading from a specific path, LoadFrom may be the correct call. The only ways to totally remove that would be to:

  • remove the requirement to load from a specific path
  • use a custom AppDomain with different probing-paths, and use MarshalByRefObject to chat between them

这在实践中也是如此,但避免了CA警告:

assembly = Assembly.Load(new AssemblyName { CodeBase = fileToLoadFullName })

Depending on what you are trying to achieve you might be able to use Assembly.Load . This way your assembly can be in the GAC or your local bin directory.

var sampleAssembly = Assembly.Load("SampleAssembly, Version=1.0.2004.0, Culture=neutral, PublicKeyToken=8744b20f8da049e3");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM