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, null, new 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
using System;
using System.Threading;
using System.Reflection;
using System.Reflection.Emit;


public class MyApplication
{
public delegate void MyEvent(Object temp);
public static void MyMain()
{
TypeBuilder helloWorldClass = CreateCallee(Thread.GetDomain());

EventInfo[] info =
helloWorldClass.GetEvents(BindingFlags.Public | BindingFlags.Instance);
Console.WriteLine("'HelloWorld' type has following events :");
for(int i=0; i < info.Length; i++)
{
Console.WriteLine(info[i].Name);
}
}

// Create the callee transient dynamic assembly.
private static TypeBuilder CreateCallee(AppDomain myDomain)
{
AssemblyName assemblyName = new AssemblyName();
assemblyName.Name = "EmittedAssembly";

// Create the callee dynamic assembly.
AssemblyBuilder myAssembly =
myDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
// Create a dynamic module named "CalleeModule" in the callee.
ModuleBuilder myModule = myAssembly.DefineDynamicModule("EmittedModule");

// Define a public class named "HelloWorld" in the assembly.
TypeBuilder helloWorldClass =
myModule.DefineType("HelloWorld", TypeAttributes.Public);

MethodBuilder myMethod1 = helloWorldClass.DefineMethod("OnClick",
MethodAttributes.Public, null, new 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");
}
// Create the events.
EventBuilder myEvent1 = helloWorldClass.DefineEvent("Click", EventAttributes.None,
typeof(MyEvent));
myEvent1.SetRaiseMethod(myMethod1);
EventBuilder myEvent2 = helloWorldClass.DefineEvent("MouseUp", EventAttributes.None,
typeof(MyEvent));
myEvent2.SetRaiseMethod(myMethod2);

helloWorldClass.CreateType();
return(helloWorldClass);
}
}


出处:https://www.cnblogs.com/Bluse/archive/2005/10/17/256645.html