我正在尝试发出以下内容:
这是一个结构,它将作为属性包含在动态发出的类中:
public struct GeoPoint
{
public double? lat { get; set; }
public double? lon { get; set; }
}
我必须发出以下课程:
public class GeoPointOwner
{
public GeoPoint GeoPoint{get; set;}
public double? Getlat ()
{
return GeoPoint.lat;
}
}
我不知道如何正确发出方法 GeoPointOwner.Getlat
这是我的代码:
var mbGet = typeBuilder.DefineMethod("Getlat", MethodAttributes.Public, typeof(double?), Type.EmptyTypes);
var genGet = mbGet.GetILGenerator();
genGet.Emit(OpCodes.Ldarg_0);
//getMethodInfo below is MethodInfo of Getter of GeoPoint property
genGet.Emit(OpCodes.Call, getMethodInfo);
var getMethod = typeof(GeoPoint).GetProperty("lat").GetGetMethod(true);
//code emitted below fails when invoked
genGet.Emit(OpCodes.Callvirt, getMethod);
genGet.Emit(OpCodes.Stloc_0);
genGet.Emit(OpCodes.Ret);