ioncodes / dnpatch

.NET Patcher library using dnlib
MIT License
313 stars 49 forks source link

Unable to cast object of type 'System.Collections.Generic.List`1[dnlib.DotNet.Emit.Instruction]' to type 'dnlib.DotNet.Emit.Instruction[]' #43

Closed gagmeng closed 7 years ago

gagmeng commented 7 years ago

The codes are below: Patcher patcher1 = new Patcher("xxxx.dll", true); Target target1 = new Target() { Namespace = "Pxxxx.App", Class = "Global", Method = "get_Code" }; Instruction[] instructions = patcher1.GetInstructions(target1); Invalid cast exception pops up just likes that: Unable to cast object of type 'System.Collections.Generic.List`1[dnlib.DotNet.Emit.Instruction]' to type 'dnlib.DotNet.Emit.Instruction[]' in dnpatch.PatchHelper.GetInstructions(Target target)

gagmeng commented 7 years ago

public Instruction[] GetInstructions(Target target) { var type = FindType(target.Namespace + "." + target.Class, target.NestedClasses); MethodDef method = FindMethod(type, target.Method, target.Parameters, target.ReturnType); return (Instruction[])method.Body.Instructions; } The above method should changed to as below: public Instruction[] GetInstructions(Target target) { var type = FindType(target.Namespace + "." + target.Class, target.NestedClasses); MethodDef method = FindMethod(type, target.Method, target.Parameters, target.ReturnType);
return new List(method.Body.Instructions).ToArray(); }

ioncodes commented 7 years ago

I'll change that today :) Thanks for the input!!