Can I run code from a .NET assembly from a command line?

隐身守侯 提交于 2019-11-28 12:16:16

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
J. Steen

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!