English 中文(简体)
Debugging error "The Type xx is defined in an assembly that is not referenced"
原标题:

The full error is as follows:

The type System.Windows.Forms.Control is defined in an assembly that is not referenced. You must add a reference to assembly System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 .

and it points at the very first statement (an Debug.Assert line) in the very first class in a library project that doesn t need System.Windows.Forms (or so I thought). I know how to solve it: add the mentioned reference. But how do I find out what library is causing this error, or better, what part of the code triggers using the WinForms library?

Normally, you can add libraries that reference others, but you only need to add references to these others when they re actually used.


EDIT: Alternative solution

This or similar problems can also be resolved using the Binding Log Viewer Fuslogvw.exe from Microsoft s Framework Tools. It shows all attempts and successes of assemblies your application binds to.

最佳回答

I suspect there s no line of your code that s causing this, since you say you aren t making use of the System.Windows.Forms types and the compiler error isn t pointing to a (useful) line of your code.

What I think is happening is that you re referencing a library which has a publicly-visible method or property that either returns a System.Windows.Forms.Control or takes one as a parameter. It doesn t matter whether you actually end up calling that method/property, the fact that it s publically visible means that your own code has to be able to resolve all the types that the library is using. If the library only used System.Windows.Forms internally, you wouldn t be experiencing this.

It also means just looking at the dependencies of the assemblies you re depending on may merely narrow down the list of suspects, since there could be some assemblies that depend on System.Windows.Forms internally (no problem) and the one troublemaking assembly that has a public parameter / return value of a type from the S.W.Forms assembly.

My suggestion is you just set up an empty project without a reference to S.W.Forms, then add each of your dependencies in turn and try to compile after each one.

问题回答

I had the same error.

The problem was that I used a reference to a project, which uses System.Windows.Forms inside.

The solution is to add a reference to System.Windows.Forms also in your project.

Use something like NDepend or Reflector or the Object Browser to check the dependencies of the assemblies you depend on.

I cannot think of any other way given the info above.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签