Dim CompTypeArray1() As Type = New Type() {GetType(System.Void)}error: 不支持Void类型。
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
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",
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
MethodAttributes.Public, null, new Type[]
{typeof(Object)});
ILGenerator methodIL1 = myMethod1.GetILGenerator();
methodIL1.Emit(OpCodes.Ret);
MethodBuilder myMethod2 = helloWorldClass.DefineMethod("OnMouseUp",
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
MethodAttributes.Public, typeof(void), new Type[]
{typeof(Object)});
ILGenerator methodIL2 = myMethod2.GetILGenerator();
methodIL2.Emit(OpCodes.Ret);
if(myMethod1.ReturnType ==myMethod2.ReturnType)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
Console.WriteLine("met1.ret = met2.ret");
}以上的这段代码说明,C#中,返回值的类型(DefineMethod的第三引数)处的null 和typeof(void)的意义是不一样的。null的时候,returntype是未定义的状态,typeof(void )才是C#中希望的void类型。
VB.NET中只用Nothing 而不能用GetType(Void).
提供测试全部代码,其实来自MSDN
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
using System;
using System.Threading;
using System.Reflection;
using System.Reflection.Emit;
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
public class MyApplication
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
public delegate void MyEvent(Object temp);
public static void MyMain()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
TypeBuilder helloWorldClass = CreateCallee(Thread.GetDomain());
![](/Images/OutliningIndicators/InBlock.gif)
EventInfo[] info =
helloWorldClass.GetEvents(BindingFlags.Public | BindingFlags.Instance);
Console.WriteLine("'HelloWorld' type has following events :");
for(int i=0; i < info.Length; i++)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine(info[i].Name);
}
}
![](/Images/OutliningIndicators/InBlock.gif)
// Create the callee transient dynamic assembly.
private static TypeBuilder CreateCallee(AppDomain myDomain)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
AssemblyName assemblyName = new AssemblyName();
assemblyName.Name = "EmittedAssembly";
![](/Images/OutliningIndicators/InBlock.gif)
// 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");
![](/Images/OutliningIndicators/InBlock.gif)
// Define a public class named "HelloWorld" in the assembly.
TypeBuilder helloWorldClass =
myModule.DefineType("HelloWorld", TypeAttributes.Public);
![](/Images/OutliningIndicators/InBlock.gif)
MethodBuilder myMethod1 = helloWorldClass.DefineMethod("OnClick",
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
MethodAttributes.Public, null, new Type[]
{typeof(Object)});
ILGenerator methodIL1 = myMethod1.GetILGenerator();
methodIL1.Emit(OpCodes.Ret);
MethodBuilder myMethod2 = helloWorldClass.DefineMethod("OnMouseUp",
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
MethodAttributes.Public, typeof(void), new Type[]
{typeof(Object)});
ILGenerator methodIL2 = myMethod2.GetILGenerator();
methodIL2.Emit(OpCodes.Ret);
if(myMethod1.ReturnType ==myMethod2.ReturnType)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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);
![](/Images/OutliningIndicators/InBlock.gif)
helloWorldClass.CreateType();
return(helloWorldClass);
}
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)