C#CodeProvider:类继承?

时间:2011-05-21 19:34:25

标签: c# inheritance scripting

所以我使用CodeProvider为我正在制作的游戏设置了一个小脚本引擎。 我已经在我的项目中定义了一些脚本需要访问的自定义类,但是,即使我添加了Assembly Reference(“NAMEOFEXE.exe”)类继承似乎也不正常。

我可以继承类,但我无法更改字段或任何内容。 EG。

项目内的代码:

public class inheritable {
   internal string _name = "NoName";
}

脚本内的代码:

public class MyClass : inheritable {
   public MyClass() {
      _name = "Custom class in script";
   }
}

在脚本编辑上,我得到:

error CS1061: 'inheritable' does not contain a definition for '_name' and no extension method '_name' accepting a first argument of type 'inheritable' could be found (are you missing a using directive or an assembly reference?)

如何解决此问题?

4 个答案:

答案 0 :(得分:2)

您是否有理由使用internalprotectedprotected internal

如果您需要公共读取访问权限和受保护的集合访问权限,则只需使用自动属性:

public string _name { get; protected set; }

您还可以在inheritable中定义一个带有名称的构造函数。

答案 1 :(得分:2)

public class inheritable {
   protected internal string _name = "NoName";
}

答案 2 :(得分:0)

在_name: -

上定义一个getter和setter
public string _name{
    get; set;
}

答案 3 :(得分:0)

当然,看起来你应该保护_name,因为你希望派生类可以访问它。我建议将它从一个字段更改为一个属性。