4

Actually it is very strange exception, because it happens only when I build the project as Release and does not happen at all when I choose Debug. In debug mode, the app works perfect and the following code works well.

Here is the code of my extension method:

public static T DeepClone<T>(this T source) where T : UIElement
{
   T result;

   // Get the type
   Type type = source.GetType();

   // Create an instance
   result = Activator.CreateInstance(type) as T; //throws exception here only if I build project as (release)

   CopyProperties<T>(source, result, type);
   DeepCopyChildren<T>(source, result);

   return result;
}

The exception is:

An exception of type 'System.MissingMethodException' occurred in System.Private.Reflection.Execution.dll but was not handled in user code

Additional information: MissingConstructor_Name, Windows.UI.Xaml.Controls.RelativePanel. For more information, visit http://go.microsoft.com/fwlink/?LinkId=623485

I've found some related questions to this exception, but they all are pointing to missing libraries or update libraries like this but didn't change anything in my app.

Community
  • 1
  • 1
Moamen Naanou
  • 1,683
  • 1
  • 21
  • 45
  • 1
    Unrelated, but `as T` is useful when the result might not be of type `T`. Functionality-wise, `x as T` means `x is T ? (T) x : null`, except that `x` is only evaluated once. You know that `x is T` is necessarily always true, and in the unlikely event that you made a mistake there somewhere, getting an exception immediately makes for easier debugging than a `NullReferenceException` somewhere inside `CopyProperties`. Therefore, I recommend `(T) Activator.CreateInstance(type)`. –  Feb 12 '17 at 09:29

1 Answers1

7

This problem is related to the fact that Release build of UWP apps uses the .NET native toolchain. In this mode reflection needs some hints to work properly. Aparently the constructor of the RelativePanel is not available for reflection.

Luckily there is a workaround for this as described in this blogpost.

In your UWP project's Properties folder is a file called default.rd.xml. Open it and add the following line inside the <Applications> element:

<Type Name="Windows.UI.Xaml.Controls.RelativePanel" 
      Dynamic="Required All" Activate="Required All" /> 

The Dynamic attribute should ensure reflection is possible and the Activate attribute should ensure the constructors are available for activation - which is key for your case.

This should include all members of RelativePanel for reflection and everything should work as expected.

You can see more details on the default.rd.xml file structure here.

Martin Zikmund
  • 38,440
  • 7
  • 70
  • 91
  • Thanks, actually I tried your fix, clean solution and build (release) but unfortunately still getting the same. – Moamen Naanou Feb 12 '17 at 09:35
  • Then I would suggest trying make a dummy a method, that will just create an instance of `RelativePanel` (`new RelativePanel()`) and maybe call a method or set a property on it. Then recompile in Release and see if the type is now present or not. – Martin Zikmund Feb 12 '17 at 09:49
  • I have updated my answer with another attribute that should actually fix your issue :-) - the `Activate` attribute. – Martin Zikmund Feb 12 '17 at 09:53
  • 1
    That's perfect, thanks a lot, after Adding (Activate) I've got exceptions for each control / element inside the relative panel (Image, Ellipse .. etc), then added all same what did with RelativePanel, then problem solved :) – Moamen Naanou Feb 12 '17 at 10:10
  • 1
    Awesome :-D ! It is probably possible to add an entire namespace by using the `Namespace` attribute, so you can save yourself some time and maintenance :-) . – Martin Zikmund Feb 12 '17 at 10:36
  • 1
    You can also use the Subtype version of the directives to apply the policy to all subtypes of the given type. – MattWhilden Mar 10 '17 at 23:25