Posted September 11, 2024Sep 11 Hi. I have problems with restoring local variable from byte array, when I get variables Signature and I try to convert them to LocalSig I get and exception because can't convert MethodSig to LocalSig. Any help would be great. { using (BinaryReader binaryReader4 = new BinaryReader(new MemoryStream(this.DataStructure.LEH))) { binaryReader4.BaseStream.Position = (long)hInt; MethodBodySize1 = binaryReader4.ReadInt32() >> 8; // 104 short LocalsSize2 = binaryReader4.ReadInt16(); short EHSize2 = binaryReader4.ReadInt16(); binaryReader4.ReadInt16(); methodInfo2.MethodData = binaryReader.ReadBytes(MethodBodySize1); var SignatureRead = SignatureReader.ReadSig(this.module, binaryReader4.ReadBytes((int)LocalsSize2)); IList<TypeSig> locals; if (SignatureRead is LocalSig) { locals = ((LocalSig)(SignatureRead)).GetLocals(); for (int k = 0; k < locals.Count; k++) methodInfo2.Method_Locals.Add(new Local(locals[k])); } else if (SignatureRead is MethodSig) { MethodSig SignatureRead_MS = SignatureRead as MethodSig; //locals = ((MethodSig)(SignatureRead)).GetLocals(); } methodInfo2.MethodEH = ((EHSize2 == 0) ? null : binaryReader4.ReadBytes((int)EHSize2)); } }
September 12, 2024Sep 12 Author Actually there is no error with the above code, it is just that bytes signatures was wrong so it will throw exception; throwing error is naturally in such case I think! Here is the final code used: IList<TypeSig> locals = ((LocalSig)SignatureReader.ReadSig(this.module, binaryReader4.ReadBytes((int)LocalsSize2))).GetLocals(); for (int l = 0; l < locals.Count; l++) { methodInfo2.Method_Locals.Add(new Local(locals[l])); }
Create an account or sign in to comment