日期:2014-05-17  浏览次数:20848 次

请问这两句IL代码用emit如何写呢?
IL代码如下


newobj instance void [mscorlib]System.Guid::.ctor(string)
call instance void [mscorlib]System.Nullable`1<valuetype [mscorlib]System.Guid>::.ctor(!0)



这样写会有报错。

Type propertyType = propertyInfo.PropertyType;
Type nullableUnderlyingType = Nullable.GetUnderlyingType(propertyType);
Type propertyBaseType = nullableUnderlyingType != null ? nullableUnderlyingType : propertyType;

il.Emit(OpCodes.Newobj, propertyBaseType.GetConstructor(new Type[] { typeof(string) }));
if (nullableUnderlyingType != null)
      il.Emit(OpCodes.Call, propertyType.GetConstructor(Type.EmptyTypes));

------解决方案--------------------
目测你这il代码是两个构造函数吗?
用refrector.exe工具反编译一下,可以生成C#或VB.net代码