VB.NET里面不允许GetType(System.Void)

 

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

error: 不支持Void类型。
ExpandedBlockStart.gif ContractedBlock.gif Type[] CompTypeArray1  =   new  Type[]  dot.gif {
ExpandedBlockEnd.gif                    
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.

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

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

提供测试全部代码,其实来自MSDN
ContractedBlock.gif ExpandedBlockStart.gif
None.gifusing System;
None.gif
using System.Threading;
None.gif
using System.Reflection;
None.gif
using System.Reflection.Emit;
None.gif
None.gif
None.gif
public class MyApplication
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
public delegate void MyEvent(Object temp);
InBlock.gif    
public static  void MyMain()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        TypeBuilder helloWorldClass 
= CreateCallee(Thread.GetDomain());
InBlock.gif
InBlock.gif        EventInfo[] info 
=
InBlock.gif            helloWorldClass.GetEvents(BindingFlags.Public 
| BindingFlags.Instance);
InBlock.gif        Console.WriteLine(
"'HelloWorld' type has following events :");
InBlock.gif        
for(int i=0; i < info.Length; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Console.WriteLine(info[i].Name);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
// Create the callee transient dynamic assembly.
InBlock.gif
    private static TypeBuilder CreateCallee(AppDomain myDomain)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        AssemblyName assemblyName 
= new AssemblyName();
InBlock.gif        assemblyName.Name 
= "EmittedAssembly";
InBlock.gif
InBlock.gif        
// Create the callee dynamic assembly.
InBlock.gif
        AssemblyBuilder myAssembly =
InBlock.gif            myDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
InBlock.gif        
// Create a dynamic module named "CalleeModule" in the callee.
InBlock.gif
        ModuleBuilder myModule = myAssembly.DefineDynamicModule("EmittedModule");
InBlock.gif
InBlock.gif        
// Define a public class named "HelloWorld" in the assembly.
InBlock.gif
        TypeBuilder helloWorldClass =
InBlock.gif            myModule.DefineType(
"HelloWorld", TypeAttributes.Public);
InBlock.gif
InBlock.gif        MethodBuilder myMethod1 
= helloWorldClass.DefineMethod("OnClick",
ExpandedSubBlockStart.gifContractedSubBlock.gif            MethodAttributes.Public, 
nullnew Type[]dot.gif{typeof(Object)});
InBlock.gif        ILGenerator methodIL1 
= myMethod1.GetILGenerator();
InBlock.gif        methodIL1.Emit(OpCodes.Ret);
InBlock.gif        MethodBuilder myMethod2 
= helloWorldClass.DefineMethod("OnMouseUp",
ExpandedSubBlockStart.gifContractedSubBlock.gif            MethodAttributes.Public, 
typeof(void), new Type[]dot.gif{typeof(Object)});
InBlock.gif        ILGenerator methodIL2 
= myMethod2.GetILGenerator();
InBlock.gif        methodIL2.Emit(OpCodes.Ret);
InBlock.gif     
InBlock.gif        
if(myMethod1.ReturnType ==myMethod2.ReturnType)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Console.WriteLine(
"met1.ret = met2.ret");
ExpandedSubBlockEnd.gif        }

InBlock.gif        
// Create the events.
InBlock.gif
        EventBuilder myEvent1 = helloWorldClass.DefineEvent("Click", EventAttributes.None,
InBlock.gif            
typeof(MyEvent));
InBlock.gif        myEvent1.SetRaiseMethod(myMethod1);
InBlock.gif        EventBuilder myEvent2 
= helloWorldClass.DefineEvent("MouseUp", EventAttributes.None,
InBlock.gif            
typeof(MyEvent));
InBlock.gif        myEvent2.SetRaiseMethod(myMethod2);
InBlock.gif
InBlock.gif        helloWorldClass.CreateType();
InBlock.gif        
return(helloWorldClass);
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
None.gif


转载于:https://www.cnblogs.com/Bluse/archive/2005/10/17/256645.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值