python调用c# dll实例

python 2.7
vs2015

image.png
namespace third_dll
{
    public abstract class AbsCore //抽象类
    {
        public abstract string methodA(); //抽象方法
        public string methodB()
        {
            return "methodB";
        }
    }

    public class AbsA : AbsCore
    {
        public override string methodA() //实现抽象方法
        {
            return "AbsA methodA";
        }
    }
    public class AbsB : AbsCore
    {
        public override string methodA() //实现抽象方法
        {
            return "AbsB methodA";
        }
        public new string methodB() //重写methodB
        {
            return "B";
        }
    }


    public interface ICore //一个接口
    {
        string test();
    }

    public class IA :ICore
    {
        private string name = "IA";

        public string test() //实现接口方法
        {
            return name;
        }
        public string getName()
        {
            return name;
        }
        public void setName(string name)
        {
            this.name = name;
        }
    }

    public class IB : ICore
    {
        private string name = "IB";

        public string test() //实现接口方法
        {
            return name;
        }
    }
}

输出动态链接库


image.png
image.png
using System;
using third_dll;

namespace csharp2dll
{
    public class Test
    {
        public static string testStatic() //测试静态方法
        {
            return "testStatic success";
        }

        public void testVoid() //测试void方法
        {
            Console.Out.WriteLine("testVoid success");
        }

        public string testString() //测试实例方法
        {
            return "testString success";
        }

        public string testString(string str1, string str2=";str2=null") //测试重载实例方法
        {
            return str1+str2;
        }

        public string testI(ICore i) //测试传入java实例,ICore的实现类型
        {
            return i.test();
        }

        public string testAbs(AbsCore abs)//测试传入java实例,AbsCore的非抽象子类
        {
            return abs.methodA();
        }

        public ICore getI() //测试返回接口的c#实例
        {
            IA i = new IA();
            i.setName("hello");
            return I;
        }

        public AbsCore getAbs()//测试返回抽象类的c#实例
        {
            AbsA abs = new AbsA();
            return abs;
        }
    }
}

输出动态链接库


image.png

编译后可得


image.png

image.png
image.png
# coding=utf-8
import clr  # pip install pythonnet

clr.AddReference('csharp2dll')
clr.AddReference('third_dll')

from csharp2dll import *
from third_dll import *

if __name__ == '__main__':
    AbsCore = AbsCore
    AbsA = AbsA
    AbsB = AbsB
    ICore = ICore
    IA = IA
    IB = IB
    Test = Test

    test = Test()
    print Test.testStatic()  # 测试静态方法
    test.testVoid()  # 测试void方法
    print test.testString()  # 测试返回string的方法
    print test.testString("aaaaaa")  # 测试重载的方法
    print test.testI(IA())  # 测试c#实例入参
    print test.testI(IB())
    print test.testAbs(AbsA())  # 测试c#实例入参
    print test.testAbs(AbsB())

    i = test.getI()  # 获得IA的实例
    print i.test()
    i.setName("c#")
    print i.getName()  # 输出c#
    j = test.getAbs()  # 获得AbsA实例
    print j.methodB()  # j的实际类型是AbsA, methodB即是AbsCore的methodB,故输出"methodB"
image.png

为了朋友们的学习,放出源码。c#以vs2015打开。python工程以pycharm打开
链接: https://pan.baidu.com/s/1pazt1UGp5DhYQYiEA3Kd5w
提取码: ig74

你可能感兴趣的:(python调用c# dll实例)