.NET source code crash on constructor? .Invoke () (empty pass)

After scratching my head for most of the day, I came across a very strange problem with .NET code compiled using .NET Native (used for Windows UWP applications).

The following code works fine in any .NET runtime, 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 the main .NET assembly, code throws an exception of type NotImplementedException

However, when the null distribution operator is deleted, it works fine on the .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

It smells like a compiler or runtime error . What's going on here? Did I miss something?

+6
1

, .

: https://github.com/dotnet/corert/issues/3565

  • ConstructorInfo.Invoke(object []) System.Reflection(C:\Program Files (x86)\Reference \Microsoft\Framework.NETPortable\v4.5\Profile\Profile78\System.Reflection.dll , Invoke .
  • - - , , . , # ​​.
  • , # ( ), ( NullReferenceException null ).
  • , NULL , # , NullReferenceException , callvirt . ConstructorInfo.Invoke( []) , .

, ConstructorInfo.Invoke( []) NetStandard 2.0 ( ). .NET Native . # callvirt , .

+2

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


All Articles