基本的に、バイト配列内のデータをオブジェクトに逆シリアル化しようとしています。文字列を読み取るために、UTF8エンコーディングのGetStringメソッドを使用しようとしています。ここに私のコードの一部があります:
var mm = new DynamicMethod("get_value", typeof(object)
, new Type[] { typeof(byte[]), typeof(int), typeof(int), typeof(int) });
var utf8 = Encoding.GetEncoding("utf-8"); //l.GetValue(null, null).GetType().GetMethod("GetString");
var getstring = utf8.GetType().GetMethod("GetString", new Type[] { typeof(byte[]), typeof(int), typeof(int) });
// var getString = Encoding.UTF8.GetType().GetMethod("GetString", new Type[] { typeof(byte[]), typeof(int), typeof(int) });
var h = new EmitHelper(mm.GetILGenerator());
var defaultCase = h.ILGenerator.DefineLabel();
var lbs = new Label[] { h.DefineLabel(),h.DefineLabel()};
var getInt32Method = typeof(BitConverter).GetMethod("ToInt32", new Type[] { typeof(byte[]), typeof(int) });
//Arg0 = Byte [] , Arg1 = type, Arg2 = size, Arg3 = offset
h
.ldarg_1
.@switch(lbs)
.MarkLabel(defaultCase)
.ldnull
.ret();
h
.MarkLabel(lbs[0])
.ldarg_0
.ldarg_3
.call(getInt32Method)
.box(typeof(int))
.ret();
//THis is the function that is causing problem; If i remove this function works;
h
.MarkLabel(lbs[1])
.ldarg_0 //Load array
.ldarg_3 //Load Offset (index)
.ldarg_2 //Load Size (count)
.callvirt(getstring)
.boxIfValueType(typeof(string))
.ret();
return (GetValueDelegate)mm.CreateDelegate(typeof(GetValueDelegate));
このコードの外で「getstring」のメソッド署名を確認したところ、動作しました。「.boxIfAny」を削除しようとしましたが、「callvirt」ではなく「call」も使用してみました。私が理解しているように、「callvirt」は、この場合に適用されるインスタンスメソッドです。何か案は?