problems in loading c# dll in python #2569
Unanswered
MCO-SOCOMEC
asked this question in
Q&A
Replies: 2 comments 4 replies
-
It seems like there is some issue with your csproj try setting it to:
hope that helps |
Beta Was this translation helpful? Give feedback.
0 replies
-
Hi, |
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi,
I'm a newbie of c# and I have a little experience with python. I tried to follow the tutorial in here
https://github.com/pythonnet/pythonnet/wiki/How-to-call-a-dynamic-library
and everything goes fine. I created a c# dll project in Visual Studio Code, I copied the "calculate" code in the Class1.cs that was automatically generated by VS Code, I generated the dll, I moved it in the folder of my python example and I was able to run the example as it is in the tutorial. Everything was fine.
Now I tried to add a new method, so I just added HelloWorld()
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CalcTestNS
{
public class calculate
{
public int Add(int a, int b)
{
return a + b;
}
}
I followed the procedure as before but now the python example fails at the
from CalcTestNS import calculate
Do you have any suggestion?
Thanks
michele
System.TypeLoadException: Non è stato possibile caricare il tipo 'System.Runtime.CompilerServices.NullableContextAttribute' dall'assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. in System.ModuleHandle.ResolveType(RuntimeModule module, Int32 typeToken, IntPtr* typeInstArgs, Int32 typeInstCount, IntPtr* methodInstArgs, Int32 methodInstCount, ObjectHandleOnStack type)
in System.ModuleHandle.ResolveTypeHandleInternal(RuntimeModule module, Int32 typeToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext)
in System.Reflection.RuntimeModule.ResolveType(Int32 metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
in System.Reflection.CustomAttribute.FilterCustomAttributeRecord(CustomAttributeRecord caRecord, MetadataImport scope, Assembly& lastAptcaOkAssembly, RuntimeModule decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, Object[] attributes, IList derivedAttributes, RuntimeType& attributeType, IRuntimeMethodInfo& ctor, Boolean& ctorHasParameters, Boolean& isVarArg)
in System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeModule decoratedModule, Int32 decoratedMetadataToken, Int32 pcaCount, RuntimeType attributeFilterType, Boolean mustBeInheritable, IList derivedAttributes,
Boolean isDecoratedTargetSecurityTransparent)
in System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeMethodInfo method, RuntimeType caType, Boolean inherit)
in System.Attribute.GetCustomAttributes(MemberInfo element, Type type, Boolean inherit)
in System.Attribute.GetCustomAttribute(MemberInfo element, Type attributeType, Boolean inherit)
in System.Reflection.CustomAttributeExtensions.GetCustomAttribute[T](MemberInfo element, Boolean inherit)
in Python.Runtime.MethodObject.AllowThreads(MethodBase[] methods)
in Python.Runtime.ClassManager.GetClassInfo(Type type, ClassBase impl)
in Python.Runtime.ClassManager.InitClassBase(Type type, ClassBase impl, ReflectedClrType pyType)
in Python.Runtime.ReflectedClrType.GetOrCreate(Type type)
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "c:\Dev\2025_soh\pyTestDll\test.py", line 12, in
from CalcTestNS import calculate
File "", line 1055, in _handle_fromlist
Python.Runtime.InternalPythonnetException: Failed to create Python type for CalcTestNS.calculate ---> System.TypeLoadException: Non è stato possibile caricare il tipo 'System.Runtime.CompilerServices.NullableContextAttribute' dall'assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
in System.ModuleHandle.ResolveType(RuntimeModule module, Int32 typeToken, IntPtr* typeInstArgs, Int32 typeInstCount, IntPtr* methodInstArgs, Int32 methodInstCount, ObjectHandleOnStack type)
in System.ModuleHandle.ResolveTypeHandleInternal(RuntimeModule module, Int32 typeToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext)
in System.Reflection.RuntimeModule.ResolveType(Int32 metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
in System.Reflection.CustomAttribute.FilterCustomAttributeRecord(CustomAttributeRecord caRecord, MetadataImport scope, Assembly& lastAptcaOkAssembly, RuntimeModule decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, Object[] attributes, IList derivedAttributes, RuntimeType& attributeType, IRuntimeMethodInfo& ctor, Boolean& ctorHasParameters, Boolean& isVarArg)
in System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeModule decoratedModule, Int32 decoratedMetadataToken, Int32 pcaCount, RuntimeType attributeFilterType, Boolean mustBeInheritable, IList derivedAttributes,
Boolean isDecoratedTargetSecurityTransparent)
in System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeMethodInfo method, RuntimeType caType, Boolean inherit)
in System.Attribute.GetCustomAttributes(MemberInfo element, Type type, Boolean inherit)
in System.Attribute.GetCustomAttribute(MemberInfo element, Type attributeType, Boolean inherit)
in System.Reflection.CustomAttributeExtensions.GetCustomAttribute[T](MemberInfo element, Boolean inherit)
in Python.Runtime.MethodObject.AllowThreads(MethodBase[] methods)
in Python.Runtime.ClassManager.GetClassInfo(Type type, ClassBase impl)
in Python.Runtime.ClassManager.InitClassBase(Type type, ClassBase impl, ReflectedClrType pyType)
in Python.Runtime.ReflectedClrType.GetOrCreate(Type type)
--- Fine della traccia dello stack dell'eccezione interna ---
in Python.Runtime.ReflectedClrType.GetOrCreate(Type type)
in Python.Runtime.ModuleObject.GetAttribute(String name, Boolean guess)
in Python.Runtime.ModuleObject.tp_getattro(BorrowedReference ob, BorrowedReference key)
Beta Was this translation helpful? Give feedback.
All reactions