Created
February 18, 2013 06:11
-
-
Save krote/4975388 to your computer and use it in GitHub Desktop.
powershell fake class sample (not worked)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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