我想在vb.net中創建一個動態對象,這將使用PHPas簡單它具有魔術方法的getter,setters和調用方法,但我需要這樣做在vb.netvb.net相當於PHP的魔術方法
在PHP:
<?php
class foo {
private $vars = array()
public function __construct() {}
public function __get($name) {
if (in_array($name, $this->vars)) {
return $this->vars[$name];
}
}
public function __set($name, $value) {
$this->vars[$name] = $value;
}
public function __call($method, $arguments) {
.....
}
public function __callStatic($method, $arguments) {
.....
}
}
?>
然後我就可以使用這個對象,如:
<?php
// Calles foo::__construct as normal
$myFoo = new foo();
// Calls the __set method parsing the values $name as "myVar1" and $value as "foo"
$myFoo->myVar1 = "foo";
// Runs the __set method parsing the values $name as "myVar2" and $value as "bar"
$myFoo->myvar2 = "bar";
// Calls the __get method parsing the value $name as "myVar1"
// and then a seccond call to __get parsing $name as "myVar2"
echo $myFoo->myVar1 . $myFoo->myVar2
// Calls __call parsing $name as "foobar" and $values as null
$myFoo->foobar();
// Calls __callStatic parsing $name as "barfoo" and $values as null
foo::barfoo();
?>
能VB.NET即使有這種編程基層應付,或只是不可能呢?
關於'get'and'set'this [answer](http://stackoverflow.com/ a/20152225/5457643)可能會有幫助。 – Turamarth
'get'和'set'部分很簡單,只需使用'Dictionary(Of String,Object)'或者通用的方式'public class Magic(Of T)... Dictionary(Of String,T)''。 'call'和'callStatic'可以通過[Reflection]來完成(http://www.codeproject.com/Articles/206412/Reflection-An-Introduction-to-Reflection-in-NE)。 –
在其他帖子上的答案不是我所要求的,我更新了我的問題,以反映這一點,並添加更多的細節。 – Chris