确定子对象的类型

时间:2012-12-07 14:17:56

标签: c# inheritance

所以,我有一个名为LogEvent的基础对象,我创建了其他派生的类,例如ExecuteEvent或SubmitEvent。当我尝试获取特定实例的类型时,它们总是以LogEvent的形式返回。以下是每个类的定义:

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

在这里,我尝试根据班级的类型做不同的事情:

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

交换机总是被传递,因为currentEvent.GetType()。ToString()总是出现在LogEvent中。

编辑:问题是我首先以不同的方式创建这些不同的类。这是错误的代码:

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

在我从eventTypeAttribute获取属性“type”的第二行,我应该获取value属性。我使用type属性来确定属性在其value属性中存储的值的类型。唉,TGIF。

3 个答案:

答案 0 :(得分:1)

如果您实际从LogEvent获取GetType的类型对象,那么您将拥有LogType类的实例,而不是任何派生类的实例。但是,我认为你没有得到你认为的结果。

ToString()对象上使用Type将返回完整的命名空间,例如"MyApp.ExecuteEvent"而不是"ExecuteEvent"

改为使用Name属性:

switch (currentEvent.GetType().Name)

答案 1 :(得分:1)

您可以使用is关键字进行类型检查,例如currentEvent is ExecuteEvent,这也可以消除拼写错误的可能性。此外,currentEvent is LogEvent将返回true,因为,就像Jared所说,它是LogEvent

答案 2 :(得分:0)

试试这个:

ExecuteEvent executeEvent = currentEvent as ExecuteEvent;
if (executeEvent != null) 
{
 item.SubItems.Add("Job executed by " + currentExEvent.executeHost);
}
相关问题