Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

problems in loading c# dll in python#2569

Unanswered
MCO-SOCOMEC asked this question inQ&A
Mar 26, 2025· 2 comments· 4 replies
Discussion options

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;
}

    public int Sub(int a, int b)    {        return a - b;    }    public string HelloWorld()    {        return "Hello World";    }}

}

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)

You must be logged in to vote

Replies: 2 comments 4 replies

Comment options

It seems like there is some issue with your csproj

try setting it to:

<Project Sdk="Microsoft.NET.Sdk">    <PropertyGroup>        <OutputType>Library</OutputType>        <TargetFramework>net6.0</TargetFramework>        <ImplicitUsings>disable</ImplicitUsings>        <Nullable>enable</Nullable>        <Platforms>x64</Platforms>    </PropertyGroup></Project>

hope that helps

You must be logged in to vote
0 replies
Comment options

Hi,
thank you very much. The problem was the .net version. I was using a net.90: moving to 6.0 as in your suggestion it works fine.
Thank you again
Michele

You must be logged in to vote
4 replies
@TonyCongqianWang
Comment options

Just fyi: 8.0 also worked for me. Might be that they also support the lts versions. I didn't find the corresponding docs either

@MCO-SOCOMEC
Comment options

Hi, I tried also 8.0 and it works.
Now I'm trying to get my dll working but I got some loading errors like

System.TypeLoadException: Non è stato possibile caricare il tipo 'System.Globalization.CultureInfo' dall'assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'

or

System.TypeLoadException: Non è stato possibile caricare il tipo 'System.ValueTuple`2' dall'assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'

Commenting some parts of the code everything works. I tried to search online and tried some suggestions like adding this in my .csproj

<assemblies>  <add assembly="System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /></assemblies>

but it doesn't work .. I'm a newbie of c#. Do you have any idea?
thank you so much
Michele

@TonyCongqianWang
Comment options

There might be a problem with your dotnet installation or your dll.

Did you usedotnet publish to create your dll? Can you share what parts of your code you commented out?

@MCO-SOCOMEC
Comment options

Hi,
I'm using visual studio code to "play" with c# and the .net environment installed on my pc. The project configuration is

Library net8.0 enable disable

One of the line that creates problem is when I parse a csv file and I do

t0 = DateTime.ParseExact(lsInputDataWithoutHeader[0].TimeStamp, "dd/MM/yyyy; HH:mm:ss:FF",
System.Globalization.CultureInfo.InvariantCulture);

I'm asking also the support of a colleague who daily uses c# with Visual Studio.
thanks for the support
Michele

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
Q&A
Labels
None yet
2 participants
@MCO-SOCOMEC@TonyCongqianWang

[8]ページ先頭

©2009-2025 Movatter.jp