Call a static method of a custom class
powershell, static-methods
Solution
The original code contains a few typos (e.g. `Class`, `sting`) and a mistake - the class has to be `public`.
Here is the corrected code which works without errors:
# the corrected code added inline (might be in a DLL, as well):
Add-Type @'
public class Student
{
public static string GetData(string id)
{
return "data1";
}
public static string GetData(string fName, string lName)
{
return "data2";
}
}
'@
# call the static method:
[Student]::GetData('myId')
Problem
I am writing a powershell script which use my own DLL in it: ``` [System.Reflection.Assembly]::LoadFile("E:\Group.School.dll") ``` I want to access a static method in `Student class`. That static method has been overloaded. ``` Class Student { public static sting GetData(string id) { .... } public static sting GetData(string fName, string lName) { .... } } ``` From PowerShell I am going to access the first method like below: ``` $data = [Group.School.Student]::GetData $data.Invoke("myId") ``` This gives me an exception saying Exception calling "Invoke" with "1" argument(s): "Exception calling "GetData" with "1" argument(s): "Object reference not set to an instance of an object.""