简体   繁体   English

C#Activator createInstance用于扩展类

[英]C# Activator createInstance for extending class

I have a base class, which is as follows: 我有一个基类,如下所示:

public Data()
    {
        id = num++;
        SetVariables();
    }
    //fill every Variable varNames, parseInduction, noise, seperator in Children Classes
    public Data(String line)
    {
        //first declare all variables in sub classes
        if (id == 0)
            throw new NotSupportedException("You are not allowed to use this constructor for creating the first instance!");
        id = num++;
        SetVariables();
        parseLine(line);
    }

And i also have a Sub Class extending this Class. 我也有一个子类扩展了这个类。

class DienstGruppe : Data
{
    protected override void SetVariables(){
        varNames = new String[] {"id", "name"};
        parseInduction = "DienstGruppen = {";
        parseEnd = "};";
        beginOfDataLine = "<";
        endOfDataLine = ">";
        noise = new String[] { "\"" };
    }
}

And i try to create an object with the Activator.CreateInstance() Function as follows: 我尝试使用Activator.CreateInstance()函数创建一个对象,如下所示:

Data myData = (Data)Activator.CreateInstance(this.GetType(), new object[] { line });

Note: this.GetType() is used in a function of Data by the extending class, to get the Type of the current class. 注意:this.GetType()由扩展类在Data函数中使用,以获取当前类的Type。

But this causes a problem. 但这会引起问题。 Bizzarly i get an error, that the class (in my case DienstGruppe) does not have the constructor. Bizzarly,我得到一个错误,该类(在我的情况下为DienstGruppe)没有构造函数。 I guess inheritance is not the same in c# as in java. 我猜想在c#中的继承与在Java中的继承是不一样的。 So how can i solve this problem? 那么我该如何解决这个问题呢?

It works for "Data" though. 它适用于“数据”。

Regards, Dominik 问候,多米尼克

In addition to the answer by Pavel, which is correct about requiring a constructor with the appropriate signature in the child class, it may be worth pointing out why you need to do that. 除了帕维尔(Pavel)的答案(在子类中要求具有适当签名的构造函数是正确的)之外,还有必要指出为什么需要这样做。

In C#, constructors are not inherited, as per Section 1.6.7.1 of the C# Language Specification. 在C#中,根据C#语言规范的Section 1.6.7.1 ,不会继承构造函数。

Unlike other members, instance constructors are not inherited, and a class has no instance constructors other than those actually declared in the class. 与其他成员不同,实例构造函数不会被继承,并且一个类除实际在类中声明的实例构造函数之外,没有其他实例构造函数。 If no instance constructor is supplied for a class, then an empty one with no parameters is automatically provided. 如果没有为一个类提供实例构造函数,那么将自动提供一个没有参数的空实例构造函数。

You seemed to think that inheritance works differently in C# than Java, but in this case Java behaves the same way. 您似乎认为继承在C#中的工作方式与Java不同,但是在这种情况下,Java的行为方式相同。 See Section 8.8 in the Java spec. 参见Java规范中的Section 8.8

Constructor declarations are not members. 构造函数声明不是成员。 They are never inherited and therefore are not subject to hiding or overriding. 它们永远不会被继承,因此不会被隐藏或覆盖。

For more information on possible reasoning behind this decision, see this StackOverflow question. 有关此决定背后可能原因的更多信息,请参见此StackOverflow问题。

You should write constructor in DienstGruppe to inherit it from base class like this: 您应该在DienstGruppe中编写构造函数,以从基类继承它,如下所示:

public DienstGruppe(String line) : base(line) { }

So it requires constructor in child class. 因此,它需要子类中的构造函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM