So, I have a base object called LogEvent and I create other classes derived from that such as an ExecuteEvent or a SubmitEvent. When I try to get the type of the specific instances they always come back as LogEvent. Here’s each of the classes’ definitions:
class LogEvent
{
public List<LogEventAttribute> eventAttributes = new List<LogEventAttribute>();
public int clusterId;
public DateTime eventTime;
public LogEvent(List<LogEventAttribute> eventAttributes)
{
this.eventAttributes = eventAttributes;
this.clusterId = Convert.ToInt32(eventAttributes.Find(p => p.name.Equals("Cluster")).value);
this.eventTime = DateTime.Parse(eventAttributes.Find(p => p.name.Equals("EventTime")).value);
}
}
class SubmitEvent : LogEvent
{
public string submitHost;
public SubmitEvent(List<LogEventAttribute> eventAttributes)
: base(eventAttributes)
{
this.submitHost = eventAttributes.Find(p => p.name.Equals("SubmitHost")).value;
}
}
class ExecuteEvent : LogEvent
{
public string executeHost;
public ExecuteEvent(List<LogEventAttribute> eventAttributes)
: base(eventAttributes)
{
this.executeHost = eventAttributes.Find(p => p.name.Equals("ExecuteHost")).value;
}
}
class TerminatedEvent : LogEvent
{
public bool successful;
public TerminatedEvent(List<LogEventAttribute> eventAttributes)
: base(eventAttributes)
{
this.successful = Convert.ToBoolean(eventAttributes.Find(p => p.name.Equals("TerminatedNormally")).value);
}
}
class LogEventAttribute
{
public string name, type, value;
public LogEventAttribute(string name, string type, string value)
{
this.name = name;
this.type = type;
this.value = value;
}
}
And here’s where I try to do different things based on the type of class:
switch (currentEvent.GetType().ToString())
{
case "ExecuteEvent":
ExecuteEvent currentExEvent = currentEvent as ExecuteEvent;
item.SubItems.Add("Job executed by " + currentExEvent.executeHost);
break;
case "SubmitEvent":
SubmitEvent currentSubEvent = currentEvent as SubmitEvent;
item.SubItems.Add("Job submitted by " + currentSubEvent.submitHost);
break;
}
The switch always gets passed over because currentEvent.GetType().ToString() always comes out to LogEvent.
Edit: The problem was that I was first creating these different classes differently. Here’s the faulty code:
LogEventAttribute eventTypeAttribute = currentEventAttributes.Find(p => p.name.Equals("MyType"));
string eventType = eventTypeAttribute.type;
switch (eventType)
{
case "SubmitEvent":
logEvents.Add(new SubmitEvent(currentEventAttributes));
break;
case "ExecuteEvent":
logEvents.Add(new ExecuteEvent(currentEventAttributes));
break;
case "TerminatedEvent":
logEvents.Add(new TerminatedEvent(currentEventAttributes));
break;
default:
logEvents.Add(new LogEvent(currentEventAttributes));
break;
}
On the second line where I’m getting the property “type” from the eventTypeAttribute, I should instead be getting the value property. I was using the type property to determine the type of the value the attribute has stored in its value property. Argh, TGIF.
If you actually get type object for
LogEventfromGetType, then you have instances of theLogTypeclass, not of any of the derived classes. However, I don’t think that you get what you think that you get.Using
ToString()on theTypeobject will return the complete namespace, e.g."MyApp.ExecuteEvent"rather than"ExecuteEvent".Use the
Nameproperty instead: