Why am I getting this definition definition error?

I am using TaskBar methods defined in the Microsoft.WindowsAPICodePack.Taskbar namespace. In particular, I will focus on SetProgressState for this question.

Here is the meta definition I get when I define the SetProgressState definition:

 namespace Microsoft.WindowsAPICodePack.Taskbar { public class TaskbarManager { public void SetProgressState(TaskbarProgressBarState state); public void SetProgressState(TaskbarProgressBarState state, IntPtr windowHandle); public void SetProgressState(TaskbarProgressBarState state, System.Windows.Window window); } } 

Obviously, I missed most of this class definition to highlight only one overload method.

At this point, I used one-parameter overload and had no problems. However, today I tried using two-parameter overload, which takes IntPtr as its second parameter.

When I did this, I started getting this error during build:

The type "System.Windows.Window" is defined in an assembly that is not a reference. You must add a reference to the assembly 'PresentationFramework, Version = 3.0.0.0, Culture = neutral, PublicKeyToken = 31bf3856ad364e35'

So my question is, why didnโ€™t I get an error for using one-parameter overload, but I get an error message to refer to one of the others (and to the wrong one)?

Edit (for additional subquery):

I also tried the following, which didn't matter:

 SetProgressState(myState, (IntPtr) myWindowHandle); 

I thought that I would explicitly abandon the compiler's confusion about implementing the corresponding overload, but that was not the case.

+4
source share
2 answers

According to the MSDN page in the "Overload Resolution" section, the compiler will start by selecting potential candidates

Each of these contexts defines a set of candidate candidate elements and a list of arguments in its own unique way.

then the best goal is chosen:

If a collection contains only one member of a function, then this member of the function is the best member of the function.

My understanding here is that the compiler does not even consider two argument methods when you call it with 1 argument. However, when you use version 2 of the arguments, it needs information about the types of arguments. In this case, he should know that System.Windows.Window is the ability to determine which overload you want to cause.

Example

Imagine you have 2 classes in separate class libraries.

 class Foo { } class Bar : Foo { } 

and 4 methods in another library

 static void Do() { } static void Do(Foo foo) { } static void Do(Bar bar) { } static Foo Get() { return new Bar(); } 

You reference a library of methods and a library containing Foo , but not a library containing Bar .

Then in your application you will get an object of type Foo from the method library (it may be Bar , but you do not know). How should the compiler resolve a possible Do() call with arguments?

It cannot if it does not have type information for the panel.


As for your subquery, this is the result of the above plus the fact that the cast does not necessarily lead to the choice of overload. Imagine that System.Windows.Window comes from IntPtr for a moment. Passing an argument to IntPtr does not help the compiler to allow overloading at all (see Example above).

Since there is no type information, the compiler throws an error because it cannot know for sure . Honestly, for compilers this feature.

+2
source

I will expand my comments here for clarity. Your project cannot find System.Windows.Window. I am mistaken in my comment when I said that you need to add:

 using System.Windows; 

To file.

Instead, the project should have a link to System.Windows. The link you want is indicated in the error message: PresentationFramework. You will also need to enable PresentationCore (a similar error will appear in which you will be asked to add a link to PresentationCore).

The type "System.Windows.Window" is defined in an assembly that is not referenced. You must add the assembly reference PresentationFramework , Version = 3.0.0.0, Culture = neutral, PublicKeyToken = 31bf3856ad364e35 '

+1
source

Source: https://habr.com/ru/post/1488310/


All Articles