C# .NET - NullRef。当尝试从列表中调用 Action<string, KeyValuePair<Action<T>>>

问题描述 投票:0回答:0

我正在尝试从

<TagDefinition>
调用 Action
List<string, KeyValuePair<Action<TagDefinition>>>
。我在这里使用一个列表,因为字典不允许重复键。目标是调用
Action<TagDefinition>
,当从我的服务器接收到
TagDefinition
列表时。

现在奇怪的部分:

我在 Unity 中运行这段代码,一切都按预期工作。当我在我的 HoloLens (UWP/ARM64) 上运行相同的代码时,我得到了一个 NullRef。当试图在“HandleIncomingTags(...)”中调用

Action<TagDefinition>
时。

 // Create the Action and assign it to List<> in "_tagClient" 

private void SubscribeTags(List<string> tags)
{
    foreach (var subscribedTag in tags)
    {
        var tagAction = new Action<TagDefinition>((tag) =>
        {
            ScriptingEngine.Instance.Invoke(tag.Name, tag.Value);
        });

        _tagClient.SubscribeTagAction(subscribedTag, tagAction);
    }
}

// TagClient 

private List<KeyValuePair<string, Action<TagDefinition>>> _tagActions = new List<KeyValuePair<string, Action<TagDefinition>>>();

public void SubscribeTagAction(string tagName, Action<TagDefinition> action)
{
    _tagActions.Add(new KeyValuePair<string, Action<TagDefinition>>(tagName, action));
}

private void HandleIncomingTags(List<TagDefinition> tags) // This method is called, when a new List<TagDefiniton> is received from the server
{
    
  var mainThread = UnityMainThread.Instance;

  foreach (var tag in tags)
  { 
      foreach (var tagAction in _tagActions) 
      {                
          if (tagAction.Key == tag.Name)
          {
              mainThread.Invoke(() => tagAction.Value.Invoke(tag)); // NullRef. Exeption here when running it on the HoloLens 2 (UWP)
          }
      }
  }
}


// TagDefinition

public class TagDefinition
{
    public string Name { get; set; } = string.Empty;
    public short Qc { get; set; } = 0;
    public long Timestamp { get; set; } = 0;
    public string SourceName { get; set; } = string.Empty;
    public string SourceSystem { get; set; } = string.Empty;
    public dynamic Value { get; set; }
    public string Type { get; set; }
}

我已经尝试调试处理程序的每一步。 TagDefinition 不为空,Action<> 不为空,tag.Value 不为空。一切看起来都很好,但我仍然得到这个 NullReference:

NullReferenceException: Object reference not set to an instance of an object.
  at System.Linq.Expressions.Interpreter.LightLambda.MakeRunDelegateCtor (System.Type delegateType) [0x00000] in <00000000000000000000000000000000>:0 
  at System.Linq.Expressions.Interpreter.LightLambda.GetRunDelegateCtor (System.Type delegateType) [0x00000] in <00000000000000000000000000000000>:0 
  at System.Linq.Expressions.Interpreter.LightDelegateCreator.CreateDelegate () [0x00000] in <00000000000000000000000000000000>:0 
  at System.Linq.Expressions.Expression`1[TDelegate].Compile (System.Boolean preferInterpretation) [0x00000] in <00000000000000000000000000000000>:0 
  at System.Runtime.CompilerServices.CallSite`1[T].CreateCustomNoMatchDelegate (System.Reflection.MethodInfo invoke) [0x00000] in <00000000000000000000000000000000>:0 
  at System.Runtime.CompilerServices.CallSite`1[T].MakeUpdateDelegate () [0x00000] in <00000000000000000000000000000000>:0 
  at System.Runtime.CompilerServices.CallSite`1[T].GetUpdateDelegate (T& addr) [0x00000] in <00000000000000000000000000000000>:0 
  at System.Runtime.CompilerServices.CallSite`1[T].GetUpdateDelegate () [0x00000] in <00000000000000000000000000000000>:0 
  at System.Runtime.CompilerServices.CallSite`1[T]..ctor (System.Runtime.CompilerServices.CallSiteBinder binder) [0x00000] in <00000000000000000000000000000000>:0 
  at System.Runtime.CompilerServices.CallSite`1[T].Create (System.Runtime.CompilerServices.CallSiteBinder binder) [0x00000] in <00000000000000000000000000000000>:0 
  at SignalRClient.ConcretizeValueType (TagDefinition tag) [0x00000] in <00000000000000000000000000000000>:0 
  at ARInsight.SceneManagement.Elements.ObjectFactory+<>c__DisplayClass8_1.<SubscribeHologramTags>b__0 (TagDefinition tag) [0x00000] in <00000000000000000000000000000000>:0 
  at System.Action`1[T].Invoke (T obj) [0x00000] in <00000000000000000000000000000000>:0 
  at SignalRClient+<>c__DisplayClass14_1.<HandleIncomingTags>b__0 () [0x00000] in <00000000000000000000000000000000>:0 
  at UnityEngine.Display+DisplaysUpdatedDelegate.Invoke () [0x00000] in <00000000000000000000000000000000>:0 
  at UnityMainThread.Update () [0x00000] in <00000000000000000000000000000000>:0 
UnityEngine.DebugLogHandler:LogException(Exception, Object)
InsightLogHandler:LogException(Exception, Object)
UnityEngine.Logger:LogException(Exception, Object)
UnityEngine.Debug:CallOverridenDebugHandler(Exception, Object)
c# .net unity3d action
© www.soinside.com 2019 - 2024. All rights reserved.