简体   繁体   English

从Ektron.Cms.Content.Targeting.Rules.RuleTemplate继承时出错

[英]Error when inheriting from Ektron.Cms.Content.Targeting.Rules.RuleTemplate

I have a C# class called MyCustomRuleTemplate which is inherited from Ektron.Cms.Content.Targeting.Rules.RuleTemplate class. 我有一个名为MyCustomRuleTemplate的C#类,该类继承自Ektron.Cms.Content.Targeting.Rules.RuleTemplate类。 In that I have added a constructor such as below 在那我添加了一个如下的构造函数

public MyCustomRuleTemplate(): base("someKey")
{
//Some code here
}

Its working fine without any error. 它的工作正常,没有任何错误。 If I given it as 如果我给它

public MyCustomRuleTemplate()
{
//Some code here
}

Im getting error like 'Ektron.Cms.Content.Targeting.Rules.RuleTemplate' does not contain a constructor that takes 0 arguments . 我收到类似'Ektron.Cms.Content.Targeting.Rules.RuleTemplate' does not contain a constructor that takes 0 arguments错误'Ektron.Cms.Content.Targeting.Rules.RuleTemplate' does not contain a constructor that takes 0 arguments Can anybody help me to know why it is? 有人可以帮助我知道为什么吗?

The reason you are seeing "does not contain a constructor that takes 0 arguments" when instantiating your class object using the second constructor is because when you call your constructor, c# tries to call the constructor on the base class as well, which in this case takes a parameter. 使用第二个构造函数实例化类对象时,看到“不包含带有0个参数的构造函数”的原因是,当您调用构造函数时,c#也会尝试在基类上调用该构造函数接受一个参数。

See this post on msdn: 请参阅msdn上的这篇文章:

http://msdn.microsoft.com/en-us/library/ms173115%28v=vs.80%29.aspx http://msdn.microsoft.com/zh-cn/library/ms173115%28v=vs.80%29.aspx

Key parts: 关键部分:

"In this example, the constructor for the base class is called before the block for the constructor is executed. The base keyword can be used with or without parameters. Any parameters to the constructor can be used as parameters to base, or as part of an expression. For more information, see base. “在此示例中,基类的构造函数在执行构造函数的块之前被调用。base关键字可以与参数一起使用,也可以不与参数一起使用。构造函数的任何参数都可以用作基础的参数,或者用作基础结构的一部分。有关更多信息,请参见base。

In a derived class, if a base-class constructor is not called explicitly using the base keyword, then the default constructor, if there is one, is called implicitly." 在派生类中,如果未使用base关键字显式调用基类构造函数,则默认构造函数(如果存在)被隐式调用。”

And: "If a base class does not offer a default constructor, the derived class must make an explicit call to a base constructor using base." 并且:“如果基类不提供默认构造函数,则派生类必须使用base显式调用基构造函数。”

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

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