简体   繁体   中英

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)

Can you change the signature of the OnAdReceivedReward?

public void OnAdReceivedReward(string adUnitId, AdSdkBase.Reward reward, AdSdkBase.AdInfo adInfo){}

Or try casting the object reward to it's structure.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM