Skip to content

Instantly share code, notes, and snippets.

@altrive
Created February 22, 2014 14:53
Show Gist options
  • Save altrive/9156060 to your computer and use it in GitHub Desktop.
Save altrive/9156060 to your computer and use it in GitHub Desktop.
function Main
{
#1.No attribute
$a = 5;
Write-Host ($a) #5
#2.With Add Value attribute
[AddValue(5)]
$b = 5;
Write-Host $b #5+5 =10
#3.Assign variable after declaration
[AddValue(5)]
$c = 1 + 1;
$c = 10 + 5
Write-Host $c #15+5 = 20
}
Add-Type -TypeDefinition @'
using System;
using System.Management.Automation;
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
public class AddValueAttribute : ArgumentTransformationAttribute
{
private int addValue;
public AddValueAttribute(int addValue)
{
this.addValue = addValue;
}
public override object Transform(EngineIntrinsics engineIntrinsics, object inputData)
{
return (int) inputData + addValue;
}
}
'@
Main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment