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
"
,
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
MethodAttributes.Public,
null
,
new
Type[]
{typeof(Object)}
);
ILGenerator methodIL1
=
myMethod1.GetILGenerator();
methodIL1.Emit(OpCodes.Ret);
MethodBuilder myMethod2
=
helloWorldClass.DefineMethod(
"
OnMouseUp
"
,
![ExpandedBlockStart.gif](/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)
![ExpandedBlockStart.gif](/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
using System;
using System.Threading;
using System.Reflection;
using System.Reflection.Emit;
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
public class MyApplication
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
{
public delegate void MyEvent(Object temp);
public static void MyMain()
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
TypeBuilder helloWorldClass = CreateCallee(Thread.GetDomain());
![InBlock.gif](/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++)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine(info[i].Name);
}
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
// Create the callee transient dynamic assembly.
private static TypeBuilder CreateCallee(AppDomain myDomain)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
AssemblyName assemblyName = new AssemblyName();
assemblyName.Name = "EmittedAssembly";
![InBlock.gif](/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");
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
// Define a public class named "HelloWorld" in the assembly.
TypeBuilder helloWorldClass =
myModule.DefineType("HelloWorld", TypeAttributes.Public);
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
MethodBuilder myMethod1 = helloWorldClass.DefineMethod("OnClick",
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
MethodAttributes.Public, null, new Type[]
{typeof(Object)});
ILGenerator methodIL1 = myMethod1.GetILGenerator();
methodIL1.Emit(OpCodes.Ret);
MethodBuilder myMethod2 = helloWorldClass.DefineMethod("OnMouseUp",
![ExpandedSubBlockStart.gif](/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)
![ExpandedSubBlockStart.gif](/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);
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
helloWorldClass.CreateType();
return(helloWorldClass);
}
}
![None.gif](/Images/OutliningIndicators/None.gif)