Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
162 views
in Technique[技术] by (71.8m points)

c# - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:emp]
PS:25 > notepad MyMathLib.cs

(…)

[C:emp]
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:emp]
PS:27 > [Reflection.Assembly]::LoadFile(“c:empMyMathLib.dll”)

GAC    Version        Location
—    ——-        ——–
False  v2.0.50727     c:empMyMathLib.dll



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

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

[C:emp]
PS:30 > $mathInstance.Product(10, 2)
20

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...