[英]C# CodeProvider: Class inheritance?
因此,我使用CodeProvider為正在制作的游戲設置了一個小腳本引擎。 我在項目中定義了一些自定義類,腳本需要訪問這些自定義類,但是即使添加了程序集引用(“ NAMEOFEXE.exe”),類繼承也似乎無法正常工作。
我可以繼承該類,但不能更改字段或任何其他內容。 例如。
項目內部的代碼:
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?)
如何解決此問題?
您使用internal
vs. protected
或protected internal
有什么原因嗎?
如果您需要公共讀取訪問權限和受保護的集合訪問權限,則只需使用自動屬性:
public string _name { get; protected set; }
您還可以在inheritable
中定義一個接受該名稱的構造函數。
public class inheritable {
protected internal string _name = "NoName";
}
在_name上定義一個getter和setter方法:
public string _name{
get; set;
}
當然,您應該使_name受保護,因為您希望派生類可以對其進行訪問。 我建議將其從字段更改為屬性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.