我可以从命令行运行.NET程序集中的代码吗?

sha*_*oth 7 .net c# command-line assemblies

我有一个.NET类库(作为.dll文件),该库包含一个带有静态方法的类.有没有办法从命令行调用该方法?

And*_*bel 14

以下是如何从Powershell加载dll并在其中调用方法的指南.

这篇文章中最重要的部分是这些命令:

[C:\temp]
PS:25 > notepad MyMathLib.cs

(…)

[C:\temp]
PS:26 > csc /target:library MyMathLib.cs
Microsoft (R) Visual C# 2005 Compiler version 8.00.50727.42
for Microsoft (R) Windows (R) 2005 Framework version 2.0.50727
Copyright (C) Microsoft Corporation 2001-2005. All rights reserved.


[C:\temp]
PS:27 > [Reflection.Assembly]::LoadFile(“c:\temp\MyMathLib.dll”)

GAC    Version        Location
—    ——-        ——–
False  v2.0.50727     c:\temp\MyMathLib.dll



[C:\temp]
PS:28 > [MyMathLib.Methods]::Sum(10, 2)
12

[C:\temp]
PS:29 > $mathInstance = new-object MyMathLib.Methods
Suggestion: An alias for New-Object is new

[C:\temp]
PS:30 > $mathInstance.Product(10, 2)
20
Run Code Online (Sandbox Code Playgroud)

  • 比我的更好的链接发现.=) (2认同)

J. *_*een 6

看看这里,也许吧?

http://blog.usepowershell.com/2009/03/exploring-the-net-framework-with-powershell-static-members-part-4/

您可以使用加载自己的程序集

[Reflection.Assembly]::LoadFile(“c:\mysource\mylib.dll”)
Run Code Online (Sandbox Code Playgroud)

如果您不能或不愿意使用Powershell,您需要使用控制台应用程序包含静态方法的调用,如davecoulter的回答中所述