简体   繁体   English

从通过反射找到的事件中获取值类型

[英]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.我正在尝试获取作为有效负载发送的值,该事件在我的程序集的 scope 之外触发的事件。 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其中 Reward 是一个结构,例如

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.因此,当Reward作为结构传递时,它会在回调中损坏。 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.如果我将Reward更改为 class,那么它会按我的预期工作,但我无法更改它,因为它来自第 3 方 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?您可以更改 OnAdReceivedReward 的签名吗?

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

Or try casting the object reward to it's structure.或者尝试将 object 奖励投射到它的结构上。

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}");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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