MissingMethodException when opening a form in the designer; working time is excellent

Let's say I have projoct A with class A that has this property:

public bool IsConnected { get { return m_isConnected; } private set { m_isConnected = value; } } 

In the same solution, I have project B that references project A and has a user control called Login. This control has this attribute:

  private A m_A = null; 

and in the login constructor I make this call:

if (m_A! = null & m_A.IsConnected) {...}

In the same project, the main form has a custom control A on it, which was added by the form designer. The program works fine, and this property is read correctly.

However, when I open the main form in the constructor, I get this execution: MissingMethodException: 'Boolean A.get_IsConnected ()'

Commenting on m_A.IsConnected, let me use the constructor, but it gets pretty annoying. And sometimes it seems that he accidentally works.

Any ideas?

+4
source share
4 answers

I have been told in the past that this.DesignMode is not always perfectly reliable. Another option you can use is preprocessor directives:

 #if DESIGN return; #else if (m_A != null && m_A.IsConnected) { /* etc. */ } #endif 

Then add a conditional compilation symbol named DESIGN and you must be golden.

0
source

Instead of commenting on this, you can use:

 if (this.DesignMode) { return; } 

or

 if (!this.DesignMode) { if (m_A != null && m_A.IsConnected) { ... } } 

Edit: I remember that I had problems with a singleton class. The problem was that the singleton was always initialized, although the constructor was never called. If I understand your problem correctly, your problem is similar. Your m_A member will never be NULL, although it should be.
I dealt with my problem with the following:
In Visual Studio, go to "Tools \ Options ... \ Debugging \ General" and disable "Enable property evaluation and other implicit function calls" and enable "Go to properties and statements (only for managed)." Perhaps this helps.

0
source

As Ari Roth noted, DesignMode is not working correctly. To compensate for this, I use this extension method:

 public static bool IsDesignTime(this Control control) { if (LicenseManager.UsageMode == LicenseUsageMode.Designtime) { return true; } if (control.Site != null && control.Site.DesignMode) { return true; } var parent = control.Parent; while (parent != null) { if (parent.Site != null && parent.Site.DesignMode) { return true; } parent = parent.Parent; } return false; } 

Use it as described by Simon Linder.

0
source

It seems that the designer is using an old version of the management DLL that does not have the IsConnected property.

MissingMethodException is not associated with m_A. The JIT compiler complains that the property is missing. If you transfer the call to m_A.IsConnected in the method, it works because this method is not called (because m_A is really null), so it is not encoded, so the IsConnected property is not required.

When you put the control on the form in VS, the constructor creates a β€œreal” instance of your control on the form, which calls the constructor, which causes the JIT compilation problem.

0
source

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


All Articles