C#Activator createInstance用于扩展类 [英] C# Activator createInstance for extending class

查看:112
本文介绍了C#Activator createInstance用于扩展类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基类,如下所示:

  public Data()
{
id = num ++;
SetVariables();
}
//填充儿童类中的每个变量varNames,parseInduction,noise,separator
public Data(String line)
{
//首先声明所有变量子类
if(id == 0)
抛出new NotSupportedException(不允许使用此构造函数创建第一个实例!);
id = num ++;
SetVariables();
parseLine(line);
}

我也有一个扩展该类的子类。

 类DienstGruppe:数据
{
受保护的重写void SetVariables(){
varNames = new String [] { id, name};
parseInduction = DienstGruppen = {;
parseEnd =};;
beginOfDataLine =<;
endOfDataLine =>;
noise = new String [] { \};
}
}

然后我尝试使用Activator.CreateInstance()函数创建一个对象,如下所示:

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

注意:this.GetType()由扩展类在Data函数中使用,以获取当前类的Type。



但这会导致问题。Bizzarly我收到一个错误,该类(在我的情况下为DienstGruppe)没有构造函数。我猜继承在c#中与在Java中是不同的。那么如何解决此问题?



虽然它适用于数据。



问候,多米尼克

解决方案

除了Pavel的答案(关于在子类中要求具有适当签名的构造函数是正确的)外,还有必要指出为什么需要这样做。 >

在C#中,根据C#语言规范的第1.6.7.1节,不会继承构造函数。


与其他成员不同,实例构造函数不会被继承,并且
类除了实例中实际声明的
之外,没有其他实例构造函数。如果没有为一个类提供实例构造函数,那么将自动提供
一个没有参数的空实例。


认为继承在C#中的工作原理与Java不同,但是在这种情况下,Java的行为方式相同。请参见Java规范中的第8.8 部分。


构造函数声明不是成员。它们永远不会被继承,因此
不会被隐藏或覆盖。


有关此决定背后可能原因的更多信息,请参见此StackOverflow问题。


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:

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.

But this causes a problem. Bizzarly i get an error, that the class (in my case DienstGruppe) does not have the constructor. I guess inheritance is not the same in c# as in 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.

In C#, constructors are not inherited, as per Section 1.6.7.1 of the C# Language Specification.

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. See Section 8.8 in the Java spec.

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.

这篇关于C#Activator createInstance用于扩展类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆