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() //實(shí)現(xiàn)抽象方法
{
return "AbsA methodA";
}
}
public class AbsB : AbsCore
{
public override string methodA() //實(shí)現(xiàn)抽象方法
{
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() //實(shí)現(xiàn)接口方法
{
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() //實(shí)現(xiàn)接口方法
{
return name;
}
}
}
輸出動態(tài)鏈接庫
image.png
image.png
using System;
using third_dll;
namespace csharp2dll
{
public class Test
{
public static string testStatic() //測試靜態(tài)方法
{
return "testStatic success";
}
public void testVoid() //測試void方法
{
Console.Out.WriteLine("testVoid success");
}
public string testString() //測試實(shí)例方法
{
return "testString success";
}
public string testString(string str1, string str2=";str2=null") //測試重載實(shí)例方法
{
return str1+str2;
}
public string testI(ICore i) //測試傳入java實(shí)例囤官,ICore的實(shí)現(xiàn)類型
{
return i.test();
}
public string testAbs(AbsCore abs)//測試傳入java實(shí)例讼呢,AbsCore的非抽象子類
{
return abs.methodA();
}
public ICore getI() //測試返回接口的c#實(shí)例
{
IA i = new IA();
i.setName("hello");
return I;
}
public AbsCore getAbs()//測試返回抽象類的c#實(shí)例
{
AbsA abs = new AbsA();
return abs;
}
}
}
輸出動態(tài)鏈接庫
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() # 測試靜態(tài)方法
test.testVoid() # 測試void方法
print test.testString() # 測試返回string的方法
print test.testString("aaaaaa") # 測試重載的方法
print test.testI(IA()) # 測試c#實(shí)例入?yún)? print test.testI(IB())
print test.testAbs(AbsA()) # 測試c#實(shí)例入?yún)? print test.testAbs(AbsB())
i = test.getI() # 獲得IA的實(shí)例
print i.test()
i.setName("c#")
print i.getName() # 輸出c#
j = test.getAbs() # 獲得AbsA實(shí)例
print j.methodB() # j的實(shí)際類型是AbsA, methodB即是AbsCore的methodB,故輸出"methodB"
image.png
為了朋友們的學(xué)習(xí),放出源碼。c#以vs2015打開两踏。python工程以pycharm打開
鏈接: https://pan.baidu.com/s/1pazt1UGp5DhYQYiEA3Kd5w
提取碼: ig74