Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call Static method in powershell

Tags:

powershell

I have third party DLL(written in c#)

That has a class called DBConnection and static method called Connect When I call that method from my C# console application like below it will work fine.

DataPackage.Db.DBConnection.Connect() 

Next I tried to do the same thing using powershell.

[DataPackage.Db.DBConnection]::Connect() 

But that gives me error

Exception calling "Connect" with "0" argument(s): "Object reference not set to an instance of an object." 

What is the reason for this and how to fix this??

like image 920
Sonali Avatar asked Aug 02 '13 09:08

Sonali


People also ask

What is a static method call?

Output. GeeksforGeeks. Static Method. Static methods are the methods in Java that can be called without creating an object of class. They are referenced by the class name itself or reference to the Object of that class.

How do you declare a static variable in PowerShell?

PowerShell provides no way to create new types that contain static variables; however, objects of such types may be provided by the host environment. Memory for creating and deleting objects containing static variables is managed by the host environment and the garbage collection system.

What is method in PowerShell?

A "method" is a set of instructions that specify an action you can perform on the object. For example, the FileInfo object includes the CopyTo method that copies the file that the FileInfo object represents. To get the methods of any object, use the Get-Member cmdlet.

What is static method in es6?

Static methods are often used to create utility functions for an application.” In other words, static methods have no access to data stored in specific objects. Note that for static methods, the this keyword references the class. You can call a static method from another static method within the same class with this.


1 Answers

I hate to drop an answer without a reference beyond personal experience, but I have never found an explanation for this behavior, so sorry this is not more informative.

When calling a parameterless method leave off the parens. So for your example do:

[DataPackage.Db.DBConnection]::Connect 

instead of ...Connect()

like image 150
kcar Avatar answered Oct 01 '22 09:10

kcar