Simply The Best.

.NET FrameWork C# VB.NET CodeDom

博客园 首页 新随笔 联系 订阅 管理

 

Dim CompTypeArray1() As Type = New Type() {GetType(System.Void)}

error: 不支持Void类型。
Type[] CompTypeArray1 = new Type[] {
                    
typeof(void)}
;

而在C#里,允许 typoof(void)

MSDN中的解释是:这个结构体使用System.Reflection的命名空间,不具有成员,不能做成Instance(实体)。

解决方法是:
            VB.NET: type = Nothing   而不能是GetType(Void) 或 GetType(System.Void)
             C#        :   type = typeof(void);


原因:VB.NET中没有返回值的方法,由关键字Sub定义成了Sub ,而不是Void类型的Function ,VB.NET中,不需要Void这个关键字,即也就是Nothing 类型的时候,就会被解释成没有返回值的Sub.

MethodBuilder myMethod1 = helloWorldClass.DefineMethod("OnClick",
            MethodAttributes.Public, 
nullnew Type[]{typeof(Object)});
        ILGenerator methodIL1 
= myMethod1.GetILGenerator();
        methodIL1.Emit(OpCodes.Ret);
        MethodBuilder myMethod2 
= helloWorldClass.DefineMethod("OnMouseUp",
            MethodAttributes.Public, 
typeof(void), new Type[]{typeof(Object)});
        ILGenerator methodIL2 
= myMethod2.GetILGenerator();
        methodIL2.Emit(OpCodes.Ret);
     
        
if(myMethod1.ReturnType ==myMethod2.ReturnType)
        
{
            Console.WriteLine(
"met1.ret = met2.ret");
        }
以上的这段代码说明,C#中,返回值的类型(DefineMethod的第三引数)处的null 和typeof(void)的意义是不一样的。null的时候,returntype是未定义的状态,typeof(void )才是C#中希望的void类型。

VB.NET中只用Nothing 而不能用GetType(Void).

提供测试全部代码,其实来自MSDN


posted on 2005-10-17 19:31  吹口琴的程序员  阅读(2683)  评论(0编辑  收藏  举报