After scratching my head for the better part of the day, I stumbled upon a very weird issue with .NET code that is compiled using .NET Native (used for Windows UWP apps).
The following code works fine in any .NET runtime environment, including Mono, Xamarin, etc. :
public class ABC {}
// ...
var constr = typeof(ABC).GetTypeInfo().DeclaredConstructors.First();
var abc = (ABC) constr?.Invoke(new object[0]);
// abc now contains an instance of ABC
On Windows UWP with .NET Native compilation, the code throws an exception of type NotImplementedException
However, when the null propagation operator is removed, it works perfectly on .NET Native:
public class ABC {}
// ...
var constr = typeof(ABC).GetTypeInfo().DeclaredConstructors.First();
var abc1 = (ABC) constr.Invoke(new object[0]);
// abc1 now contains an instance of ABC
// the following line throws an exception on .NET Native
// but it works fine on any other .NET runtime
var abc2 = (ABC) constr?.Invoke(new object[0]);
The line in the stack trace where the exception occurs is:
at System.Reflection.ConstructorInfo.Invoke(Object[] parameters)
in f:\dd\ndp\fxcore\CoreRT\src\System.Private.Reflection\src\System\Reflection\ConstructorInfo.cs:line 41
This smells like a bug in the compiler or runtime. What's going on here? Am I missing something?
constr.Invoke()
it works perfectly. When callingconstr?.Invoke()
it throws an exception – Geehan