How do I work around the error "ByRef return value not supported in reflection invocation" in C#? -


I have a net library provided by a third party. I reflected on any of their classes and one member method Found signature was ...

Byte & amp; FooBar ()

Therefore, I wanted to call this method through reflection and got the exception "Reflection in charge" byRef return value is not supported.

'I have tried ...

var strm = new to their class (); Var t = strm.GetType (); Var MS = T.GetMember (BindingFlag.static | BindingFlag.Instance | Binding FlagsNonPublic | Binding FlagsPublic); Forchach (MS in var M) {Debug.WriteLine (String.Format ("name: {0}: {1}", m.Name, m.ToString ())); // ... // Name: Fubar: Byte & amp; Fulbar () // ...} var meth = t.GetMethod ("Fobar"); Object return value = meth.Invoke (strm, new object [] {});

I have tried to call the parameter function with ref parameter, but it did not make any difference.

I want to work around this exception C #.

To the comments: Here is how it can be done with CIL, which is generated from C # can be done.

I was hoping to use a DynamicMethod , but I can not manage to get this work without making a custom rep type on the runtime, so Instead of me, AssemblyBuilder instead.

  using the system; Using the system. Using the system. Reflection.imit; Public Representative Zero Callbaid Function (Representative De, Callback C); Public representative zero callback (ref INTAI); Fixed class program {static int i; Fixed object badmeeth () {returns returns; } Fixed MethodInfo GetBadMethod () {return type (program) .GetMethod ("BadMethod", BindingFlags.Static | BindingFlags.NonPublic); } Static zero main () {var badMethod = GetBadMethod (); Var Assembly = AssemblyBuilder.DefineDynamicAssembly (new assembly name ( "-", AssemblyBuilderAccess.Run); Var module = Vidhansbakdifaid Daynamicmodul ( "-"); Var badDelegate = module.DefineType ( "BadDelegateType", TypeAttributes.Public | TypeAttributes. Class | TypeAttributes.Sealed, typeof (Maltikastdileget)); Var badDelegateCtor = badDelegate.DefineConstructor (MethodAttributes.Public | MethodAttributes.pecialName | MethodAttributes.RTSpecialName, Colingsnmelnon. standard, a new type [] {type (object), Taipf (IntPtr)} ); Bddelgtectorksetimplementtionflags (Methodinpltributskruntime | MethodImplAttributeskmanejd); var Bddelgteinvoke = Bddelgtekdefinemethoda ( "Invoke" Methodaatributskpblik | MethodAttributeskwarchual | MethodAttr Ibutes.NewSlot | Special Features.HideBySig, typef (int) .micabffile type (), type.act type); BadDelegateInvoke.SetImplementationFlags (MethodImplAttributes.Runtime | MethodImplAttributes.Managed); var badDelegateType = badDelegate.CreateType (); Var method = module.DefineGlobalMethod ("-", MethodAttributes.Public | MethodAttributes.Static, typeof (zero), new [] {type (representative), typef (callback)}); Var il = method.GETILGenerator (); Il.Emit (OpCodes.Ldarg_1); Il.Emit (OpCodes.Ldarg_0); IL Amit (op.Seeds.Cast class, Bad Delegate); IL Amit (op.Seeds.Collvert, Bad Delegate Invoke); IL Amit (Opsods. Callwort, typef (callback). GetMathi ("Invec"); Il.Emit (OpCodes.Ret); Module.CreateGlobalFunctions (); Var callbadfunction = (callbaidfunction) representative. Prepare (typef (callbaidfunction), module. Gatemath ("-"); CallBadFunction (badMethod.CreateDelegate (badDelegateType)), (ref int i) = & gt; {I ++;}); }}  

After compiling this program, use ILDASM to isolate it, and the definition of BadMethod

 < Code>. Private Letbasig Stable Int 32 & amp; BadMethod () cil Managed {ldsflda int32 program :: i ret}  

This changes a function to return to int32 and , which will then manage the following code Pu ka ra no Allows only for C # int32 and type function parameters ( Ref Int ), so to be able to use the results, I use a callback function , Which is the return value BadMethod .


Comments

Popular posts from this blog

import - Python ImportError: No module named wmi -

Editing Python Class in Shell and SQLAlchemy -

c# - MySQL Parameterized Select Query joining tables issue -