Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 810
sharptooth Avatar asked Jul 25 '11 08:07

sharptooth


3 Answers

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
like image 152
Anders Abel Avatar answered Sep 19 '22 13:09

Anders Abel


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

like image 40
J. Steen Avatar answered Sep 22 '22 13:09

J. Steen


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.

like image 24
davecoulter Avatar answered Sep 18 '22 13:09

davecoulter