问题
My project structure
- A.dll, which has PostSharp installed via NuGet and uses it.
- B.exe, which references A and does not use PostSharp.
Edit: I created a test solution to see if I could recreate the issue, and the error went away, so it appears to be some other build-time process in the original project. I don't know yet what.
My problem
Currently this causes a compilation error for B.
Unknown build error, 'Cannot resolve dependency to assembly 'PostSharp, Version=3.0.40.9, Culture=neutral, PublicKeyToken=b13fd38b8f9c99d7' because it has not been preloaded. When using the ReflectionOnly APIs, dependent assemblies must be pre-loaded or loaded on demand through the ReflectionOnlyAssemblyResolve event.'
Installing PostSharp to B causes a new warning.
The module 'B.exe' does not contain any aspect or other transformation. For improved build-time performance, consider disabling PostSharp for this module by setting the compilation symbol (aka constant) 'SkipPostSharp' in your project, or set the MSBuild property 'SkipPostSharp=True'.
If I disable PostSharp in the project properties, I now get this error.
#error: 'PostSharp is not introduced in the build process. If NuGet just restored the PostSharp package, you need to rebuild the solution.'
This comes from RequiresPostSharp.cs, which is installed in every PostSharp project.
My goal
I need to eliminate all errors and warnings.
My proposed solution
I think if I can eliminate the first error and the need to add PostSharp to B, everything will be fine. I don't know how to fix it though.
My questions
- Is the correct/recommended solution to not install PostSharp to B, and fix the first error?
- If so, how do I do this? If not, how would I go about the proper solution?
回答1:
The described build error can be reproduced when the project B is WPF application. In this case the problem will be caused by the XAML compiler - it tries to load assemblies referenced by your dependencies (A) and fails if not successful.
To avoid the build error you need to add PostSharp reference to your B project as well. You will, of course, get the mentioned warning message then.
Starting with version 3.1, PostSharp does not add RequiresPostSharp.cs file to your project anymore, and performs validation inside msbuild script instead. Thus, you can safely disable PostSharp in the project B properties. This way you will avoid both error and warning messages.
来源:https://stackoverflow.com/questions/19823460/cannot-resolve-dependency-to-assembly-postsharp-because-it-has-not-been-preloa