Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将多个类转换为子类的最佳方法_C#_.net - Fatal编程技术网

C# 将多个类转换为子类的最佳方法

C# 将多个类转换为子类的最佳方法,c#,.net,C#,.net,我试图找出创建/转换多个类并在适当的子类下创建它们的最简单方法 下面是我的事件类的代码 public class Event { public Dictionary<string, string> dictionary = new Dictionary<string, string>(); public Event(string[] headers, string[] values) { if (headers.Length !=

我试图找出创建/转换多个类并在适当的子类下创建它们的最简单方法

下面是我的事件类的代码

public class Event
{
    public Dictionary<string, string> dictionary = new Dictionary<string, string>();

    public Event(string[] headers, string[] values)
    {
        if (headers.Length != values.Length)
            throw new Exception("Length of headers does not match length of values");

        for (int i = 0; i < values.Length; i++)
            dictionary.Add(headers[i], values[i]);
    }

    public override string ToString()
    {
        return dictionary.ToString();
    }

    public string getTimeStamp()
    {
        return DateTime.Parse(dictionary["event_ts"]).ToLocalTime().ToString();
    }

    public string getKey()
    {
        return dictionary["hbase_key"];
    }

    public string getEventType()
    {
        return dictionary["event_type"];
    }

}
除此之外,我将有多个类,它们是事件类的扩展,为不同类型的事件定义更多的方法。但是,我需要一种基于EventType的简单方法来创建适当的类。例如,如果event_type=testEvent,我将需要使用testEvent类。是否有某种方法可以放在Event中,解析Event_类型并确定应该创建哪个类


我得到的所有信息都来自于解析一个CSV文件,文件头作为第一行,值是特定行的值。

您可以使用反射将类类型与事件类型匹配,然后使用以下命令实例化它:

Activator.CreateInstance(eventType) as Event

您可以使用“工厂方法”

public Event CreateEvent(string sEventType)
{
    if (sEventType.Equals("Event1"))
        return new Event1();
    if (sEventType.Equals("Event2"))
        return new Event2();
    if (sEventType.Equals("Event3"))
        return new Event3();
    //and so on...
}

Event1、Event2和Event3是您的子类。您需要解析并调用这种方法。

使用Factory模式可能适合您的需要-请参阅这篇相关文章。看在上帝的份上,读这篇文章。另外,请使用属性而不是get/set方法,这不是像Java这样的野蛮语言,没有对属性的一流支持。您能解释一下为什么需要子类吗。听起来您的事件类就像一个数据行。因此,您从csv中提取数据,现在有了一个具有标题数组和值数组的类。您需要在专用类中封装什么行为?你能构建一个类似于策略模式或状态模式的东西来处理你检索到的数据吗?非常感谢,我花了一点时间研究如何在我的案例中使用这个方法。然而,这是处理约400个事件的最佳方法,如果用另一种方法处理,则需要相同数量的if语句。