pylhb4csharp 1.0.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
Non-Commercial Use License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Mr.Lee
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software for non-commercial purposes only, including without limitation
|
|
8
|
+
the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
9
|
+
and/or sell copies of the Software, subject to the following conditions:
|
|
10
|
+
|
|
11
|
+
1. The above copyright notice and this permission notice shall be included in all
|
|
12
|
+
copies or substantial portions of the Software.
|
|
13
|
+
|
|
14
|
+
2. Commercial use of the Software is expressly prohibited without prior written
|
|
15
|
+
permission from the copyright holder. For purposes of this license, commercial
|
|
16
|
+
use means any use of the Software that is intended for or directed toward
|
|
17
|
+
commercial advantage or monetary compensation.
|
|
18
|
+
|
|
19
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
20
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
21
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
22
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
23
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
24
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
25
|
+
SOFTWARE.
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
|
+
Name: pylhb4csharp
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Mr.Lee's CSharp Helpers
|
|
5
|
+
Author: SoftGod4MrLi
|
|
6
|
+
Author-email: SoftGod4MrLi <596928288@qq.com>
|
|
7
|
+
License: Non-Commercial Use License
|
|
8
|
+
|
|
9
|
+
Copyright (c) 2025 Mr.Lee
|
|
10
|
+
|
|
11
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
12
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
13
|
+
in the Software for non-commercial purposes only, including without limitation
|
|
14
|
+
the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
15
|
+
and/or sell copies of the Software, subject to the following conditions:
|
|
16
|
+
|
|
17
|
+
1. The above copyright notice and this permission notice shall be included in all
|
|
18
|
+
copies or substantial portions of the Software.
|
|
19
|
+
|
|
20
|
+
2. Commercial use of the Software is expressly prohibited without prior written
|
|
21
|
+
permission from the copyright holder. For purposes of this license, commercial
|
|
22
|
+
use means any use of the Software that is intended for or directed toward
|
|
23
|
+
commercial advantage or monetary compensation.
|
|
24
|
+
|
|
25
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
26
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
27
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
28
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
29
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
30
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
31
|
+
SOFTWARE.
|
|
32
|
+
Requires-Dist: pythonnet>=3.1.0
|
|
33
|
+
Requires-Python: >=3.12
|
|
34
|
+
Project-URL: Homepage, https://github.com/SoftGod4MrLi/pylhb4csharp
|
|
35
|
+
Project-URL: Documentation, https://github.com/SoftGod4MrLi/pylhb4csharp
|
|
36
|
+
Project-URL: Repository, https://github.com/SoftGod4MrLi/pylhb4csharp
|
|
37
|
+
Project-URL: Issues, https://github.com/SoftGod4MrLi/pylhb4csharp/issues
|
|
38
|
+
Description-Content-Type: text/markdown
|
|
39
|
+
|
|
40
|
+
# pylhbr4csharp
|
|
41
|
+
|
|
42
|
+
[](https://pypi.org/project/pylhbr4csharp/)
|
|
43
|
+
[](https://github.com/SoftGod4MrLi/pylhbr4csharp)
|
|
44
|
+
[](https://github.com/SoftGod4MrLi/pylhbr4csharp/blob/main/LICENSE)
|
|
45
|
+

|
|
46
|
+
[](https://github.com/SoftGod4MrLi/pylhbr4csharp)
|
|
47
|
+
|
|
48
|
+
pylhbr4csharp 是我在工作过程中陆续整理的一个 Python C# 工具包,里面就放在一个关于C#操作的类及相关函数。与其说是一个正式的开源项目,不如说是我自己的“代码杂物间”——只不过把它打包了一下,方便在不同项目之间复用。
|
|
49
|
+
|
|
50
|
+
> 由于是个人使用为主,很多设计可能带着比较强的个人习惯,也未必是最优解。如果您发现了问题或有更好的建议,非常欢迎指正。
|
|
51
|
+
|
|
52
|
+
## 安装
|
|
53
|
+
|
|
54
|
+
```
|
|
55
|
+
pip install pylhbr4csharp
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## 🌺mycsharp模块
|
|
59
|
+
|
|
60
|
+
执行C#源码
|
|
61
|
+
|
|
62
|
+
使用示例:
|
|
63
|
+
|
|
64
|
+
```
|
|
65
|
+
from pylhbr4csharp.mycsharp import MyCSharp
|
|
66
|
+
|
|
67
|
+
if __name__ == '__main__':
|
|
68
|
+
code = """
|
|
69
|
+
using System;
|
|
70
|
+
namespace MyTest
|
|
71
|
+
{
|
|
72
|
+
public class SayHelp
|
|
73
|
+
{
|
|
74
|
+
public string SayHello(string name)
|
|
75
|
+
{
|
|
76
|
+
return "Hello " + name;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
"""
|
|
81
|
+
dlls=["System.dll","mscorlib.dll"]
|
|
82
|
+
mycsharp=MyCSharp(code,dlls)
|
|
83
|
+
if mycsharp.compiler():
|
|
84
|
+
result=mycsharp.callMethod("SayHelp","SayHello",['李生',])
|
|
85
|
+
print(result)
|
|
86
|
+
else:
|
|
87
|
+
count,errMsg=mycsharp.getErrors()
|
|
88
|
+
print(count)
|
|
89
|
+
print(errMsg)
|
|
90
|
+
```
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# pylhbr4csharp
|
|
2
|
+
|
|
3
|
+
[](https://pypi.org/project/pylhbr4csharp/)
|
|
4
|
+
[](https://github.com/SoftGod4MrLi/pylhbr4csharp)
|
|
5
|
+
[](https://github.com/SoftGod4MrLi/pylhbr4csharp/blob/main/LICENSE)
|
|
6
|
+

|
|
7
|
+
[](https://github.com/SoftGod4MrLi/pylhbr4csharp)
|
|
8
|
+
|
|
9
|
+
pylhbr4csharp 是我在工作过程中陆续整理的一个 Python C# 工具包,里面就放在一个关于C#操作的类及相关函数。与其说是一个正式的开源项目,不如说是我自己的“代码杂物间”——只不过把它打包了一下,方便在不同项目之间复用。
|
|
10
|
+
|
|
11
|
+
> 由于是个人使用为主,很多设计可能带着比较强的个人习惯,也未必是最优解。如果您发现了问题或有更好的建议,非常欢迎指正。
|
|
12
|
+
|
|
13
|
+
## 安装
|
|
14
|
+
|
|
15
|
+
```
|
|
16
|
+
pip install pylhbr4csharp
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## 🌺mycsharp模块
|
|
20
|
+
|
|
21
|
+
执行C#源码
|
|
22
|
+
|
|
23
|
+
使用示例:
|
|
24
|
+
|
|
25
|
+
```
|
|
26
|
+
from pylhbr4csharp.mycsharp import MyCSharp
|
|
27
|
+
|
|
28
|
+
if __name__ == '__main__':
|
|
29
|
+
code = """
|
|
30
|
+
using System;
|
|
31
|
+
namespace MyTest
|
|
32
|
+
{
|
|
33
|
+
public class SayHelp
|
|
34
|
+
{
|
|
35
|
+
public string SayHello(string name)
|
|
36
|
+
{
|
|
37
|
+
return "Hello " + name;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
"""
|
|
42
|
+
dlls=["System.dll","mscorlib.dll"]
|
|
43
|
+
mycsharp=MyCSharp(code,dlls)
|
|
44
|
+
if mycsharp.compiler():
|
|
45
|
+
result=mycsharp.callMethod("SayHelp","SayHello",['李生',])
|
|
46
|
+
print(result)
|
|
47
|
+
else:
|
|
48
|
+
count,errMsg=mycsharp.getErrors()
|
|
49
|
+
print(count)
|
|
50
|
+
print(errMsg)
|
|
51
|
+
```
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "pylhb4csharp"
|
|
3
|
+
version = "1.0.0"
|
|
4
|
+
description = "Mr.Lee's CSharp Helpers"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
license = {file = "LICENSE"}
|
|
7
|
+
authors = [
|
|
8
|
+
{ name = "SoftGod4MrLi", email = "596928288@qq.com" }
|
|
9
|
+
]
|
|
10
|
+
requires-python = ">=3.12"
|
|
11
|
+
dependencies = [
|
|
12
|
+
"pythonnet>=3.1.0",
|
|
13
|
+
]
|
|
14
|
+
|
|
15
|
+
[project.urls]
|
|
16
|
+
Homepage = "https://github.com/SoftGod4MrLi/pylhb4csharp"
|
|
17
|
+
Documentation = "https://github.com/SoftGod4MrLi/pylhb4csharp"
|
|
18
|
+
Repository = "https://github.com/SoftGod4MrLi/pylhb4csharp"
|
|
19
|
+
Issues = "https://github.com/SoftGod4MrLi/pylhb4csharp/issues"
|
|
20
|
+
|
|
21
|
+
[project.scripts]
|
|
22
|
+
pylhb4csharp = "pylhb4csharp:main"
|
|
23
|
+
|
|
24
|
+
[build-system]
|
|
25
|
+
requires = ["uv_build>=0.11.18,<100.12.0"]
|
|
26
|
+
build-backend = "uv_build"
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import argparse
|
|
2
|
+
# Mr.Lee's Module
|
|
3
|
+
from .mycsharp import MyCSharp
|
|
4
|
+
|
|
5
|
+
__all__ = [
|
|
6
|
+
"MyCSharp"
|
|
7
|
+
]
|
|
8
|
+
|
|
9
|
+
def main() -> None:
|
|
10
|
+
parser = argparse.ArgumentParser(description="Mr.Lee's CSharp Helpers")
|
|
11
|
+
parser.add_argument('-v', '--version', action='store_true', help='show version')
|
|
12
|
+
args = parser.parse_args()
|
|
13
|
+
|
|
14
|
+
# show version
|
|
15
|
+
if args.version:
|
|
16
|
+
try:
|
|
17
|
+
from importlib.metadata import version, PackageNotFoundError
|
|
18
|
+
# 直接从包元数据获取版本号
|
|
19
|
+
ver = version("pylhb4csharp")
|
|
20
|
+
print(f"pylhb4csharp {ver}")
|
|
21
|
+
except PackageNotFoundError:
|
|
22
|
+
print("Package not found.")
|
|
23
|
+
except ImportError:
|
|
24
|
+
print("Import errored.")
|
|
25
|
+
except:
|
|
26
|
+
print("Other errored.")
|
|
27
|
+
return
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
"""
|
|
2
|
+
模块:mycsharp
|
|
3
|
+
作者:李生
|
|
4
|
+
描述:Python直接执行C#源码
|
|
5
|
+
"""
|
|
6
|
+
import clr
|
|
7
|
+
import System
|
|
8
|
+
from System import AppDomain
|
|
9
|
+
from System.Reflection import Assembly
|
|
10
|
+
|
|
11
|
+
class MyCSharp:
|
|
12
|
+
"""Python直接执行C#源码"""
|
|
13
|
+
def __init__(self, code:str, dlls:list|None = None):
|
|
14
|
+
"""
|
|
15
|
+
Args:
|
|
16
|
+
code:C#源码
|
|
17
|
+
dlls:需要加载的程序集
|
|
18
|
+
"""
|
|
19
|
+
self.code=code
|
|
20
|
+
if dlls:
|
|
21
|
+
self.dlls=dlls
|
|
22
|
+
else:
|
|
23
|
+
self.dlls=["System.dll","mscorlib.dll"]
|
|
24
|
+
|
|
25
|
+
# 编译
|
|
26
|
+
def compiler(self):
|
|
27
|
+
"""编译"""
|
|
28
|
+
from System.CodeDom.Compiler import CodeDomProvider
|
|
29
|
+
provider = CodeDomProvider.CreateProvider("CSharp")
|
|
30
|
+
compilerParams = System.CodeDom.Compiler.CompilerParameters()
|
|
31
|
+
compilerParams.GenerateInMemory = True
|
|
32
|
+
# 添加引用DLL
|
|
33
|
+
for dll in self.dlls:
|
|
34
|
+
compilerParams.ReferencedAssemblies.Add(dll)
|
|
35
|
+
self.compilerResulter = provider.CompileAssemblyFromSource(compilerParams, self.code)
|
|
36
|
+
if self.compilerResulter.Errors.HasErrors:
|
|
37
|
+
return False
|
|
38
|
+
return True
|
|
39
|
+
|
|
40
|
+
def getErrors(self) -> tuple[int,str]:
|
|
41
|
+
"""
|
|
42
|
+
获取编译错误
|
|
43
|
+
Returns:
|
|
44
|
+
错误数
|
|
45
|
+
错误信息
|
|
46
|
+
"""
|
|
47
|
+
if self.compilerResulter:
|
|
48
|
+
if self.compilerResulter.Errors.HasErrors:
|
|
49
|
+
errMsg = ""
|
|
50
|
+
for error in self.compilerResulter.Errors:
|
|
51
|
+
errMsg += f"Line {error.Line}: {error.ErrorText}\n"
|
|
52
|
+
return self.compilerResulter.Errors.Count,errMsg
|
|
53
|
+
else:
|
|
54
|
+
return 0,""
|
|
55
|
+
else:
|
|
56
|
+
return 1,"请先编译。"
|
|
57
|
+
|
|
58
|
+
def callMethod(self,className,methodName,args:list):
|
|
59
|
+
"""
|
|
60
|
+
调用方法
|
|
61
|
+
Args:
|
|
62
|
+
className:类名
|
|
63
|
+
methodName:方法
|
|
64
|
+
args:参数
|
|
65
|
+
Returns:
|
|
66
|
+
是否成功
|
|
67
|
+
返回值
|
|
68
|
+
"""
|
|
69
|
+
if self.compilerResulter:
|
|
70
|
+
assembly = self.compilerResulter.CompiledAssembly
|
|
71
|
+
calcType = assembly.GetType(className)
|
|
72
|
+
instance = System.Activator.CreateInstance(calcType)
|
|
73
|
+
return True,calcType.GetMethod(methodName).Invoke(instance, args)
|
|
74
|
+
else:
|
|
75
|
+
return False,""
|