【问题标题】:Accessing .net core dll using python使用python访问.net core dll
【发布时间】:2019-08-06 18:44:22
【问题描述】:

我需要访问一个内置于 .net core(.NETCore 版本 = v1.1)中的 C# dll。我尝试了以下方式,但出现导入错误。

import clr

clr.AddReference("dllname") - No error

from dllname import *

出现导入错误异常,提示没有名为 dllname 的模块。

注意:我在 Iron python 和 python 中都试过,都给了我同样的例外。

【问题讨论】:

    标签: c# .net python-2.7 .net-core windows-7


    【解决方案1】:

    我在 Fedora 29 上试用,使用 mono 5.18、python3.7 和 netcore 3.0.100-preview-009812, 如果您使用绝对路径来解析 netcore dll,似乎可以工作

    import clr
    import os
    
    clr.AddReference(os.path.abspath('./bin/Debug/netstandard2.0/sample.dll'))
    import sample
    p = sample.Person(name='Peter')
    

    netcore 项目就是这样生成的

    dotnet new classlib -o sample
    

    人物类

    using System;
    
    namespace sample
    {
        public class Person
        {
                public string Name { get; set; }
        }
    }
    

    更新

    根据@SMHP 提供的数据,主 .NET 框架/mono(pythonnet 运行时)与面向 .netcoreapp 2.0 的库似乎不兼容。

    【讨论】:

    • 感谢您的评论,我试过了,但没有为我工作。
    • 只是为了获取更多信息。您正在尝试哪个操作系统?什么python版本?您可以尝试以前的最小样本吗?
    • 我刚刚在 Windows 10 中使用 python2.7 和 netcore 2.2 进行了尝试,效果很好。您能否提供有关您尝试加载的 dll 的更多信息?另一个同名的python模块之间是否可能存在某种冲突?
    • 我已经安装了 pythonnet 2.3.0 来访问 .net dll,还安装了 .net core sdk 版本 2.2.105。我确认该 dll 的名称中没有现有的 python 模块。我得到了这个来自其他团队的 dll,我相信它是内置 64 位计算机的。
    • // xxxx, Version=1.1.0.0, Culture=neutral, PublicKeyToken=null // 全局类型: // 架构:AnyCPU(首选 64 位) // 运行时:.NET 4.0 [程序集:CompilationRelaxations(8)] [程序集:RuntimeCompatibility(WrapNonExceptionThrows = true)] [程序集:TargetFramework(".NETCoreApp,Version=v2.1", FrameworkDisplayName = "")]
    【解决方案2】:

    您收到导入错误,因为您在导入语句中使用了 dllname。而不是使用 dllname 使用 dll 的命名空间。 P.S:不要对dll和命名空间使用相同的名称,在python中导入时会抛出错误

    import clr
    
    clr.AddReference("dllname") - No error
    
    from namespace import *

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-12-29
      • 1970-01-01
      • 1970-01-01
      • 2011-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多