mercredi 10 août 2022

Getting value type from event found via reflection

I'm trying to get values that were sent as payload with an event that is triggered outside the scope of my assembly. I'm finding an event via reflection.

var eventInfo = adCallbackType.GetEvent(eventInfoName);

Then subscribe my method to this event.

var eventDelegate = Delegate.CreateDelegate(eventInfo.EventHandlerType, adToBindTo, methodInfo);
eventData.EventInfo.AddEventHandler(adCallbackType, eventData.Delegate);

This is the method I'm subscribing to that event

public void OnAdReceivedReward(string adUnitId, object reward, object adInfo)
{
   Debug.Log($"id: {adUnitId}");
   Debug.Log($"reward: {reward}"); //this comes corrupted, prints only the Label value
   Debug.Log($"info: {adInfo}");
}

I'm not providing the complete code here.

The problem is that one of the events has these type parameters

public static event Action<string, AdSdkBase.Reward, AdSdkBase.AdInfo> OnAdReceivedRewardEvent

where Reward is a struct, for example

public struct Reward
{
   public string Label;
   public int Amount;

   public override string ToString()
   {
      return $"Reward: {Label}, {Amount}";
   }
}

So, when Reward is passed as a struct, then it comes corrupted in the callback. If I'd change Reward to be a class instead, then it works as I expected, but I can't change it, since it comes from 3rd party SDK. I did not do much with this type of stuff before. Does it has something to do with data marshaling or am I missing something when I'm searching for method and event via reflection? (like binding attributes)





Aucun commentaire:

Enregistrer un commentaire