Call generic method with 'out' parameter - powershell

I have a 'SourceClass' class with the simple C# method:
public bool TryGetSection<T>(out T result) where T : class, new()
{ //do something and return true/false }
and I'd like to execute it from powershell code.
$_config = New-Object [SourceClass]
$method = [SourceClass].GetMethod("TryGetSection")
$genericMethod = $method.MakeGenericMethod([AuthenticationProviderTypeConfig])
$authenticationProviderTypeConfig = New-Object AuthenticationProviderTypeConfig
$genericMethod.Invoke($_config, $authenticationProviderTypeConfig)
$authenticationProviderType = $authenticationProviderTypeConfig.Type
After execution, I'm expecting that the $authenticationProviderType will be filled, but it isn't. I'm just getting an empty string. I suppose I miss something.
Is it possible to call generic method that has 'out' parameter?
Thanks.

Related

How to use delegates inside a static PowerShell class?

I want to use a func delegate in my static PowerShell 5.0 class:
I had issues to find a way to assign other static class methods for my delegate.
This code is working but not very convinient.
Is there a better way to use a delegate here ?
And I need to instanciate my static! class, only to get the type.
I tried the outcommented line, how you would do it with .NET types, but it's not working for my own class.
How can I get the type of my static class here more elegant ?
And, BTW, GetMethod() did not accecpt the BindingFlags parameter, why ?
class Demo
{
hidden static [object] Method_1([string] $myString)
{
Write-Host "Method_1: $myString"
return "something"
}
hidden static [object] Method_2([string] $myString)
{
Write-Host "Method_2: $myString"
return $null
}
hidden static [object] TheWrapper([string]$wrappedMethod, [string] $parameter)
{
# do a lot of other stuff here...
#return [System.Type]::GetType("Demo").GetMethod($wrappedMethod).CreateDelegate([Func``2[string, object]]).Invoke($parameter)
return [Demo]::new().GetType().GetMethod($wrappedMethod).CreateDelegate([Func``2[string, object]]).Invoke($parameter)
}
static DoWork()
{
Write-Host ([Demo]::TheWrapper('Method_1', 'MyMessage'))
[Demo]::TheWrapper('Method_2', 'c:\my_file.txt')
}
}
[Demo]::DoWork()
You don't need to create an instance of [demo] since [demo] is the actual type of the class. Also, you can write the delegate type more simply as [Func[string,object]]. This simplifies the body of TheWrapper method to
return [Demo].GetMethod($wrappedMethod).CreateDelegate([Func[string, object]]).Invoke($parameter)
but a much simpler way to do this in PowerShell is to get the method by passing its name to the '.' operator then invoking the result:
return [demo]::$wrappedMethod.Invoke($parameter)
In PowerShell, the right-hand side of the '.' operator doesn't need to be a constant. You can use an expression that results in the name of the method (or property) to retrieve.

Set a property for a PowerShell class on Instantiation

Is it possible to have the value of a property of a PowerShell class defined on instantiation without using a constructor?
Let's say there's a cmdlet that will return Jon Snow's current status (alive or dead). I want that cmdlet to assign that status to a property in my class.
I can do this using a constructor, but I'd like this to happen regardless of which constructor is used, or even indeed if one is used at all.
function Get-JonsCurrentStatus {
return "Alive"
}
Class JonSnow {
[string]
$Knowledge
[string]
$Status
#Constructor 1
JonSnow()
{
$this.Knowledge = "Nothing"
$this.Status = Get-JonsCurrentStatus
}
#Constructor 2
JonSnow([int]$Season)
{
if ($Season -ge 6)
{
$this.Knowledge = "Still nothing"
$this.Status = Get-JonsCurrentStatus #I don't want to have to put this in every constructor
}
}
}
$js = [JonSnow]::new()
$js
Unfortunately, you cannot call other constructors in the same class with : this() (though you can call a base class constructor with : base())[1]
Your best bet is a workaround with a (hidden) helper method:
function Get-JonsCurrentStatus {
return "Alive"
}
Class JonSnow {
[string]
$Knowledge
[string]
$Status
# Hidden method that each constructor must call
# for initialization.
hidden Init() {
$this.Status = Get-JonsCurrentStatus
}
#Constructor 1
JonSnow()
{
# Call shared initialization method.
$this.Init()
$this.Knowledge = "Nothing"
}
#Constructor 2
JonSnow([int]$Season)
{
# Call shared initialization method.
$this.Init()
if ($Season -ge 6)
{
$this.Knowledge = "Still nothing"
}
}
}
$js = [JonSnow]::new()
$js
[1] The reason for this by-design limitation, as provided by a member of the PowerShell team is:
We did not add : this() syntax because there is a reasonable alternative that is also somewhat more intuitive syntax wise
The linked comment then recommends the approach used in this answer.
You can initialise class properties on instantiation this way:
$jon = new-object JonSnow -Property #{"Status" = Get-JonsCurrentStatus; "Knowledge" = "Nothing"}

A property referencing another property in a PowerShell Class

Is it possible to have a property in a class reference another? For example, something like this. I've tried a few ways and I'm now not sure if I can do this...:
class TestClass {
[string]
$SQLInstanceName
[string]
$Server = "$($env:COMPUTERNAME)\$SQLInstanceName"
[string]myResult()
{
return $this.Server
}
}
....Thanks
Yes. Here it is implemented in your class definition:
class TestClass {
[string]
$SQLInstanceName
hidden $_Server = $($this | Add-Member ScriptProperty 'Server' `
{
# get
"$($env:COMPUTERNAME)\$($this.SQLInstanceName)"
}
)
[string]myResult()
{
return $this.Server
}
}
To see this working, new up an instance and assign a value to SQLInstanceName.
$c = [TestClass]::new()
$c.SQLInstanceName = 'MyDB'
Then invoking
$c.Server
$c.myResult()
results in
ComputerName\MyDB
ComputerName\MyDB
You should be using $this if you want to refer to a non-static property/method in the same object just like you have done with your myResult() method. Also your current sample has no default value or constructor so the SQLInstanceName is blank so just adding $this, without setting the variable, might give you misleading results. The following example might be something to consider but it is flawed.
class TestClass {
[string]$SQLInstanceName = "Test"
[string]$Server = "$($env:COMPUTERNAME)\$($this.SQLInstanceName)"
[string]myResult()
{
return $this.Server
}
}
$tcobject = New-Object TestClass
$tcobject.myResult()
However this does not work if you change the SQLInstanceName property since you are just setting default values. Classes in v5 don't really have get and set truly implemented in the same way as a .Net class so you would have to roll your own solution for that as discussed in this answer but also on this blog about v5 classes in general.
So a simple solution would work like this to get what I think you want.
class TestClass {
[string]
$SQLInstanceName = "Test"
[string]
$Server = $env:COMPUTERNAME
[string]myResult()
{
return "$($this.Server)\$($this.SQLInstanceName)"
}
}
$tcobject = New-Object TestClass
$tcobject.SQLInstanceName = "server\prod"
$tcobject.myResult()
This would be a design choice but I would not be trying to dynamically change one property based on the value of another in this case. Since you are using a value of them combined a simple method could work.

Powershell: supply only an N-th parameter when calling a ComObject function [duplicate]

I have a c# method I am loading from a dll with optional string arguments that default to null. For example
public void foo(string path, string new_name = null, bool save_now = true)
{
if(name == null)
new_name = Path.GetFileNameWithoutExtension(path);
...
if(save_now)
Save();
}
I want to call this from within a powershell script and not supply a value for new_name but one for save_now. As per this seemingly very similar question I have tried
$default = [type]::Missing
$obj.foo($path, $default, $false)
but this results in new_name being set as "System.Reflection.Missing" within the function.
Additionally I tried
$obj.foo($path, $null, $false)
but this results in new_name being set to the empty string, still not null. I could set the default to the empty string, but I was wondering if there was any good way to actually have the default value be used.
No can do in PowerShell. It doesn't support C#/VB optional parameters. It is the duty of the language calling the method to provide the default values when the programmer doesn't and PowerShell just doesn't do that.
You can simply omit the optional parameters in the call. I modified your example to run it in PS. For example:
$c = #"
public static class Bar {
public static void foo(string path, string new_name = null, bool save_now = true)
{
System.Console.WriteLine(path);
System.Console.WriteLine(new_name);
System.Console.WriteLine(save_now);
}
}
"#
add-type -TypeDefinition $c
[Bar]::Foo("test",[System.Management.Automation.Language.NullString]::Value,$false)
This generates the following
test
False
Test was passed explicitly, null is null and had no output, and the save_now evaluated to the default of True.

array_map cant found function in Bootstrap

I created a function in Bootstrap file and I want call it with array_map function.
but it show me this error :
Warning: array_map() expects parameter 1 to be a valid callback, function 'rootCreator' not found or invalid function name in ...
This is my code that call array_map :
$controls = array('products','productsubcat','newssubcat');
array_map('rootCreator', $controls);
This is my function code:
public function rootCreator($cotroller)
{
$frontController = Zend_Controller_Front::getInstance();
$defaults = array('module'=>'default' ,'controller'=> $cotroller , 'action'=>'info' );
$productRoute = new Zend_Controller_Router_Route($cotroller . '/:id/:title', $defaults);
$router = $frontController->getRouter();
$router->addRoute($cotroller, $productRoute);
}
If your function is a method of an object, you need to pass the object too.
Try
array_map(array($this, 'rootCreator'), $controls);
If it's not on the same object, you need to pass it an instance. For static methods, you can pass the class name.