none
How to access values of original type passed into a generic type?

    Question

  • Here is the code for an event handler that receives the type:

    private void HandleDialogEvent(object sender, EventArgs e)
            {
                EventObject<NpcResponse> response = sender as EventObject<NpcResponse>;
    
                //NpcResponse npcResponse = response as NpcResponse;
            }

    I need to get the values contained within the response object, but I am unable to cast to the original object and I have found now way to access the values I need. Pleas see the attached screenshots to see where the values are located within the object.

    Wednesday, April 19, 2017 9:20 PM

Answers

  • I'm not sure of the best way to go about this, because I can't see the wider context of what you are trying to achieve, but here is a simple console application that demonstrates one possible solution to your immediate problem. It requires a change to the EventObject class. I strongly suspect, but don't know for sure, that you should also add the new property to the IEvent interface (it depends on your exact user case).

    namespace ConsoleApplication1
    {
       class Program
       {
          static void Main(string[] args)
          {
             NpcResponse npcIn = new NpcResponse();
             npcIn.Event = "PlayerStat";
             npcIn.Keyword = "Love";
             npcIn.Response = "Thank you.";
    
             EventObject<NpcResponse> sender = new EventObject<NpcResponse>(npcIn);
    
             NpcResponse npcOut = sender.Event as NpcResponse;
    
             if (npcOut != null)
             {
                Console.WriteLine(npcOut.Event);
                Console.WriteLine(npcOut.Keyword);
                Console.WriteLine(npcOut.Response);
             }
    
    
          }
        
       }
    
    
       public class EventObject<T> : IEvent 
       {
          public EventType Type { get; set; }
          private T eventObject;
    
          public object Event { get { return eventObject; } } // You need to add this property to get access to the object.
    
    
          public EventObject(T eventObj)
          {
             Type = EventType.Dialog;
             eventObject = eventObj;
          }
       }
    
       public interface IEvent
       {
          EventType Type { get; set; }
       }
    
       public class NpcResponse
       {
          public string Keyword { get; set; }
          public string Response { get; set; }
          public string Event { get; set; }
       }
    
       public enum EventType { Dialog, SomethingElse }
    
    }
    

    • Marked as answer by darrenlc Thursday, April 20, 2017 1:35 AM
    Thursday, April 20, 2017 1:14 AM

All replies

  • I'm not exactly certain, but it should be something like this.

    EventObject<NpcResponse> response = sender as EventObject<NpcResponse>;
    
    NpcResponse npc = response.eventObject as NpcResponse;


    • Edited by Ante Meridian Wednesday, April 19, 2017 11:16 PM Typo.
    Wednesday, April 19, 2017 11:15 PM
  • Thanks for the answer, but I can't get to eventObject that way. Also, it seems that eventObject is an interface of type IEvent, which I can cast the response object to. Unfortunately, I am still unable to get to the values that I need.

    • Edited by darrenlc Wednesday, April 19, 2017 11:49 PM
    Wednesday, April 19, 2017 11:45 PM
  • If eventObject is of type IEvent, and IEvent has Event, Keyword, and Response, and if you have control over the code for the EventObject<T> class, then, you can make Event, KeyWord, and Response properties of the EventObject<T> class and access them that way.

    If you show us the code for the EventObject<T> class, we may be able to be a bit more specific. (If the code is very long, just show the highlights - declaration, constructors, the eventObject property.)

    Wednesday, April 19, 2017 11:57 PM
  • Here is the code for EventObject<T>:

    namespace Assets.Events
    {
        public class EventObject<T> : IEvent
        {        
            public EventType Type { get; set; }
            private T eventObject;
    
            public EventObject(T eventObj)
            {
                Type = EventType.Dialog;
                eventObject = eventObj;
            }
        }
    }

    Here is IEvent:

    namespace Assets.Events
    {
        public interface IEvent
        {
            EventType Type { get; set; }
        }
    }

    And here is NpcResponse:

    namespace Assets.Dialog.DataModels
    {
        public class NpcResponse
        {
            public string Keyword { get; set; }
            public string Response { get; set; }
            public string Event { get; set; }
        }
    }


    • Edited by darrenlc Thursday, April 20, 2017 12:28 AM
    Thursday, April 20, 2017 12:27 AM
  • I'm not sure of the best way to go about this, because I can't see the wider context of what you are trying to achieve, but here is a simple console application that demonstrates one possible solution to your immediate problem. It requires a change to the EventObject class. I strongly suspect, but don't know for sure, that you should also add the new property to the IEvent interface (it depends on your exact user case).

    namespace ConsoleApplication1
    {
       class Program
       {
          static void Main(string[] args)
          {
             NpcResponse npcIn = new NpcResponse();
             npcIn.Event = "PlayerStat";
             npcIn.Keyword = "Love";
             npcIn.Response = "Thank you.";
    
             EventObject<NpcResponse> sender = new EventObject<NpcResponse>(npcIn);
    
             NpcResponse npcOut = sender.Event as NpcResponse;
    
             if (npcOut != null)
             {
                Console.WriteLine(npcOut.Event);
                Console.WriteLine(npcOut.Keyword);
                Console.WriteLine(npcOut.Response);
             }
    
    
          }
        
       }
    
    
       public class EventObject<T> : IEvent 
       {
          public EventType Type { get; set; }
          private T eventObject;
    
          public object Event { get { return eventObject; } } // You need to add this property to get access to the object.
    
    
          public EventObject(T eventObj)
          {
             Type = EventType.Dialog;
             eventObject = eventObj;
          }
       }
    
       public interface IEvent
       {
          EventType Type { get; set; }
       }
    
       public class NpcResponse
       {
          public string Keyword { get; set; }
          public string Response { get; set; }
          public string Event { get; set; }
       }
    
       public enum EventType { Dialog, SomethingElse }
    
    }
    

    • Marked as answer by darrenlc Thursday, April 20, 2017 1:35 AM
    Thursday, April 20, 2017 1:14 AM