Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

453
Views
Invoke MethodInfo with void* as a parameter

The problem

Theres an internal method i need to invoke via reflection. It looks like this...

internal void SetComponentDataRaw(Entity entity, int typeIndex, void* data, int size)

Obtaining the MethodInfo is no problem, but calling it is one. I need to pass a void pointer to it, but such a void pointer can NOT be cast to an object. When i box that pointer, i receive an error later on that tells me it can not unbox it properly.


var SetComponentDataRawInfo = typeof(...).GetMethod("SetComponentDataRaw", ...);
var entity = someEntityToPass;
var cmp = new StructToPassToTheMethodAsAPointer();

// My first attempt, not possible to cast
unsafe {
   var handle = GCHandle.Alloc(cmp, GCHandleType.Pinned);
   var adress = handle.AddrOfPinnedObject();
   var ptr = adress.ToPointer();
   var sizeOfComponent = UnsafeUtility.SizeOf(type);

   var parameters = new [] {entity, cmpType.TypeIndex, (object)ptr, sizeOfComponent}; // (object)ptr not possible
   SetComponentDataRawInfo.Invoke(em, parameters);
   handle.Free();
}


// Second attempt
unsafe {
   var handle = GCHandle.Alloc(cmp, GCHandleType.Pinned);
   var adress = handle.AddrOfPinnedObject();
   var ptr = adress.ToPointer();
   var sizeOfComponent = UnsafeUtility.SizeOf(type);

   var parameters = new [] {entity, cmpType.TypeIndex, Pointer.Box(ptr, typeof(void*)), sizeOfComponent};
   SetComponentDataRawInfo.Invoke(em, parameters);
   handle.Free();
}

The second attempt is able to invoke the method... but i receive the following exception : ArgumentException: Object of type 'System.Reflection.Pointer' cannot be converted to type 'System.Void*'.

The question

How do we invoke a MethodInfo with a pure void* as an parameter ? Glad for any help on this topic !

over 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!