tags:

views:

67

answers:

5

Hi,

If I write a class library in Visual Studio, is it possible to write a console application or powershell script which can call the methods or set/get the properties?

This is useful for testing APIs without having to create a form with loads of buttons etc.

I actually meant my executing my own class libraries. So if I write a class library with a namespace of a.b with a class called c and a method in c, called test(), I'd want to execute this from a console app or PS.

Thanks

A: 

Yes, you can write an application that calls methods from a library.

Anon.
+2  A: 

It's easy to do from a Console Application -

Just add a reference to your class library, and use the types as needed. Just be aware you'll only be able to access your public API, since it's a separate assembly.

However, for testing purposes, you might want to consider using a testing framework. Visual Studio includes one (in Pro+ SKUs).

Reed Copsey
+3  A: 

PowerShell makes it even easier than a console app, just open the PowerShell console and new up your object. You can then set properties, call methods, etc.

    $request = [Net.HttpWebRequest]::Create("http://somewhere/")
    $request.Method = "Get"
    $request.KeepAlive = $true
    $response = $request.GetResponse()
    $response.Close()

I should be clear this is really most useful for doing spot checking or trying out a new API. In most cases using some kind of unit testing framework (MSTest, NUnit, etc), as others have mentioned, will give you better return on your time investment.

technophile
+1  A: 

Yes Powershell is able to call methods and set properties of .Net classes directly.

$conn = new-object System.Data.SqlClient.SqlConnection($connstr)
$conn.Open()
$comm = new-object System.Data.SqlClient.SqlCommand()
$comm.Connection = $conn
$comm.CommandText = $sqlstr
$dr = $comm.ExecuteReader()
while($dr.Read())
{
   //...
}
$dr.Close()
$conn.Close()
Spencer Ruport
+1  A: 

As an alternative to Powershell, You can use IronPython:

import clr
clr.AddReference("MyLibrary");

foo = MyNamespace.MyClass()
foo.Property = "something"
print foo.GetSomeValue()
Jimmy