惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
I
InfoQ
Engineering at Meta
Engineering at Meta
D
DataBreaches.Net
L
LangChain Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Recent Announcements
Recent Announcements
GbyAI
GbyAI
爱范儿
爱范儿
Microsoft Security Blog
Microsoft Security Blog
腾讯CDC
美团技术团队
罗磊的独立博客
Microsoft Azure Blog
Microsoft Azure Blog
WordPress大学
WordPress大学
T
The Blog of Author Tim Ferriss
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
雷峰网
雷峰网
M
MIT News - Artificial intelligence
D
Docker
MongoDB | Blog
MongoDB | Blog
F
Fortinet All Blogs
博客园 - 叶小钗

博客园 - 神游虚空

解决 GDAL 用 C# 访问时,中文乱码问题。 学习AI编程 安装 RAGFlow 安装 KTransformer 量化投资赚的哪方面的钱? 使用 Open XML SDK 实现 html 富文本转换为 docx 格式示例 在python中 先调用C#的dll 返回了一个类的列表,如何把这个列表转换为 python 中的 DataFrame? vue项目服务器部属源码调试解决办法 vue3 + ts + element_plus 创建后台管理系统,学习记录 大A指数周内效应分析 两个通达信公式 如何在.netcore 上实现 Rbac 权限管理 修道是精神上的减熵过程 BPMN2.0 Specifaction 节选翻译,10.4.6 事件处理(handling Events,原文 275页) BPMN2.0 Specifaction 节选翻译,10.4 事件 Events (原文233页) 使用ef或dbcontext的时候主要注意三个问题 EFCore 中 group 分组后,只选出每组中含有最大值的数据行 BPMN2.0 Specifaction 节选翻译,10.2.8 循环特征 Loop Characteristics (原文 189页) BPMN2.0 Specifaction 节选翻译,10.2.5 子流程 Sub-Processes (原文173页) BPMN2.0 Specifaction 节选翻译,10 Process (原文145页)
python中调用 .net 的 dll 时,报错:Exception has occurred...
神游虚空 · 2024-11-23 · via 博客园 - 神游虚空

1、遇到问题

     在 python3 中想调用 .net8 的 dll 。在运行时报错:

Exception has occurred: TypeLoadException
未能从程序集“System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”中加载类型“System.Runtime.CompilerServices.NullableContextAttribute”。
   在 System.ModuleHandle.ResolveType(RuntimeModule module, Int32 typeToken, IntPtr* typeInstArgs, Int32 typeInstCount, IntPtr* methodInstArgs, Int32 methodInstCount, ObjectHandleOnStack type)
   在 System.ModuleHandle.ResolveTypeHandleInternal(RuntimeModule module, Int32 typeToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext)
   在 System.Reflection.RuntimeModule.ResolveType(Int32 metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
   在 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)

  

2、解决办法

     这是因为无法加载 .netcore 的运行库。可以用如下方式加载对应的运行库:其中的目录改为你自己的内容。

from clr_loader import get_coreclr
from pythonnet import set_runtime

rt = get_coreclr(runtime_config="E:/Working/NTrade_New/PyNTrade/bin/Debug/net8.0/PyNTrade.runtimeconfig.json")
set_runtime(rt)

import clr
import sys

sys.path.append(r"E:/Working/NTrade_New/PyNTrade/bin/Debug/net8.0")
clr.AddReference(r"PyNTrade")

from PyNTrade import TestClass

tt = TestClass()
sum = tt.Add(1, 2)
print(sum)