研究Int32&的时候,无意中发现C#里面还有4个Undocument Keyword, 分别是__makeref, __reftype, __refvalue 以及__arglist。 其中前三个keyword可以这样用:

Undocumented Keywords in C#        int i = 1;
Undocumented Keywords in C#        
Undocumented Keywords in C#        TypedReference tr 
= __makeref(i);
Undocumented Keywords in C#        Type t 
= __reftype(tr); //t = System.Int32
Undocumented Keywords in C#
        
Undocumented Keywords in C#        
int i1 = __refvalue(tr, int); //i1 = 1
Undocumented Keywords in C#
        int i2 = (int)TypedReference.ToObject(tr); //i2 = 1
Undocumented Keywords in C#
        
Undocumented Keywords in C#        i
++//i = 2
Undocumented Keywords in C#
        
Undocumented Keywords in C#        
int i3 = __refvalue(tr, int); //i3 = 2

(关于TypedReference类型MSDN是这样描述的:Describes objects that contain both a managed pointer to a location and a runtime representation of the type that may be stored at that location. 同时TypedReference有[CLSCompliant(false)]标记)

于是我们可以用下面这种方法来模拟ByRef的参数

Undocumented Keywords in C#public class MyClass
}

比较不爽的就是我们必须在Foo方法体中判断TypedReference包含的类型。

注意如果把Foo写成public static void Foo(ref TypedReference tr),编译器会抱怨说:Method or delegate parameter cannot be of type 'ref System.TypedReference'。

至于__arglist则可以模拟params关键字,抄个例子:

Undocumented Keywords in C#public void Function(__arglist) 
}

调用它:Function(__arglist(2,3,4));  输出2,3,4

后注:这4个keyword毕竟是undocument的,微软也没有提供任何支持,所以不排除以后被delete掉。Mono下面的编译器似乎也不支持,不过我在vs2005 beta里面试验还是有效的。

 Updated: Flier Lu在他的blog对__arglist的使用作了更深入的探讨,推荐阅读。 :)

相关文章: