.Net core 3.1 using netstandard polyfill causes error.

Environment

  • Pythonnet version: NuGet v 2.5.1 (pythonnet_netstandard_py37_linux)
  • Python version: 3.7
  • Operating System: Linux (ubuntu 18.04)

Details

  • Trying to embed python in a c# core 3.1 console app. Within python I wanted to attach a delegate to an event on a c# object. The creation of the delegate type in DelegateManager.cs GetDispatcher calls TypeBuilder.CreateType. In .net standard, this calls the polyfill. Under core 3.1 a polyfill shouldn't be necessary. However, this polyfill appears to be defined incorrectly. It's defined as:
public static Type CreateType(this TypeBuilder typeBuilder)
{
    return typeBuilder.GetTypeInfo().GetType();
}

However, the actual method in 3.1 is this:
public Type CreateType() => (Type) this.CreateTypeInfo();

The difference being that the extra GetType() results in us returning the equivalent of typeof(System.Reflection.Emit.TypeBuilder). Which then fails to create with the arguments of a delegate.

I don't have one offhand, but I will work on a pull reqeust unless someone has some advise for how to avoid the issue. For a pull request, my plan is to #if the polyfill for !NETCOREAPP3_1

  • If there was a crash, please include the traceback here.

Not quite as useful as what I have above, but here is the traceback:

Unhandled exception. System.MissingMethodException: Constructor on type 'System.Reflection.Emit.TypeBuilder' not found.
   at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture)
   at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
   at System.Activator.CreateInstance(Type type, Object[] args)
   at Python.Runtime.DelegateManager.GetDelegate(Type dtype, IntPtr callable)
   at Python.Runtime.EventObject.AddEventHandler(IntPtr target, IntPtr handler)
   at Python.Runtime.EventBinding.nb_inplace_add(IntPtr ob, IntPtr arg)
   at Python.Runtime.Runtime.PyRun_String(String code, IntPtr st, IntPtr globals, IntPtr locals)
   at Python.Runtime.PyScope.Exec(String code, IntPtr _globals, IntPtr _locals)
   at Python.Runtime.PyScope.Exec(String code, PyDict locals)
   at Oreo.Middleware.Calibration.Program.Main(String[] args) in /home/user/dev/Oreo/Oreo/src/Oreo.Middleware.Calibration/Program.cs:line 24

Note that I'm trying to create a delegate, and it's having a hard time finding the appropriate constructor on System.Reflection.Emit.TypeBuilder.