Skip to content

Instantly share code, notes, and snippets.

@krote
Created February 18, 2013 06:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krote/4975388 to your computer and use it in GitHub Desktop.
Save krote/4975388 to your computer and use it in GitHub Desktop.
powershell fake class sample (not worked)
# classutil.ps1
# source from http://mojibake.seesaa.net/article/56484188.html
# class to cls
# var to mem
# def to func
$global:__cls__=@{}
# this function is not worked. why...
function global:cls([string] $name, [ScriptBlock] $definition)
{
$global:__cls__[$name] = $definition
}
function global:new([string] $typename)
{
function mem([string] $name, $value = $null){
$this | Add-Member NoteProperty $name $value
}
function func([string] $name, [ScriptBlock] $method) {
$this | Add-Member ScriptMethod $name $method
}
$definition = $global:__cls__[$typename]
if( !$definition ){
throw $typename + " is undefined."
}
$obj = New-Object PSObject
$obj | Add-Member ScriptMethod "__func__" $definition
$obj.__func__()
$obj.psobject.members.remove("__func__")
$obj
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment