我可以從命令行運行 .NET 程序集的代碼嗎? (Can I run code from a .NET assembly from a command line?)


問題描述

我可以從命令行運行 .NET 程序集的代碼嗎? (Can I run code from a .NET assembly from a command line?)

I have a .NET class library (as a .dll file) and that library contains a class with a static method. Is there a way to call that method from a command line?

‑‑‑‑‑

參考解法

方法 1:

Here is a guide on how to load a dll from Powershell and call methods in it.

The most important part of the post are these commands:

[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

方法 2:

Have a look here, maybe?

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

And you can load your own assembly using

[Reflection.Assembly]::LoadFile(“c:\mysource\mylib.dll”)

If you're unable or unwilling to use Powershell, you need to wrap the call for your static method with a console application, as stated in davecoulter's answer

方法 3:

Yes ‑‑ but you'll have to have a program with a Main() method that references that .dll and can call it‑‑ say in a console application.

(by sharptoothAnders AbelJ. Steendavecoulter)

參考文件

  1. Can I run code from a .NET assembly from a command line? (CC BY‑SA 3.0/4.0)

#command-line #assemblies #.net #C#






相關問題

從 FFMPEG for android 的命令行版本訪問網絡攝像頭麥克風 (Accessing webcam mic from command line version of FFMPEG for android)

為什麼我的 IDE 能找到 JAR 而我的命令行卻沒有? (Why does my IDE find the JAR but my command line doesn't?)

sbt 相當於 maven exec 插件 (sbt equivalent of maven exec plugin)

無法從 Python 的命令行導入 json (Not able to import json from commandline for Python)

在命令行上使用 VS2008 部署 SharePoint 失敗 (SharePoint deployment failure using VS2008 on the comannd line)

使用 PHP 的網頁截圖? (Web Page Screenshots with PHP?)

在命令行(終端)上使用 R 腳本的最佳方式是什麼? (What's the best way to use R scripts on the command line (terminal)?)

如何創建指向 mysql 的符號鏈接?(Mac, XAMPP) (How do I create a symbolic link to mysql? (Mac, XAMPP))

如何通過終端或其他方式將參數傳遞給 JavaScriptCore/Console? (How to pass an argument to JavaScriptCore/Console via terminal, or otherwise?)

shell命令中“&&”的目的是什麼? (What is the purpose of "&&" in a shell command?)

如何使用 dotnet test 命令行運行單個 xunit C# 測試 (How to run a single xunit C# test using dotnet test command line)

在 centOS 6.9 (Final) 中將 php-5.6 升級到 7.3 時顯示多個錯誤 (Showing multiple errors while upgrading php-5.6 to 7.3 in centOS 6.9 (Final))







留言討論