[英]How to compile and run C# code at runtime where the compiled code can access the rest of the program
我有一個C#游戲,我希望允許我們的腳本編寫小代碼文件,如下面的內容,以便我們可以修補和修改我們的一些系統。
namespace Game.Creatures
{
public class Zombie : Creature
{
public override float MovementRange { get { return 12; } }
public override float AttackDamage { get { return 5; } }
public override float TurnSpeed { get { return 2; } }
public override float WalkSpeed { get { return 1.7f; } }
public override float RunSpeed { get { return 2.5f; } }
public override float TimeBetweenAttacksSeconds { get { return 0; } }
public override bool CanAttack { get { return false; } }
public Zombie(Vector3 in_Position, Vector3 in_Scale)
{
Health = MaxHealth = 40;
CurrentAi = new Ai_Simple(this) {RecalcPathIn = -1};
}
public override void Update(GameTime in_GameTime) { base.Update(in_GameTime); }
public override void Render(GameTime in_GameTime) { base.Render(in_GameTime); }
}
}
正如您所看到的,該文件使用游戲中的類型和方法,這是可能的,還是任何運行時創建的代碼都需要限制在自身?
獎金,如果它也適用於360。
編輯:Paint.net有一個代碼實驗室插件,正是這樣做,所以我知道它必須是可能的。
是否可以動態編譯和執行C#代碼片段? 處理同樣的事情。 能夠引用您的類只意味着您要引用的其中一個程序集應該是包含Creature
的程序集。 例如
var parameters = new CompilerParameters(
new[] { "mscorlib.dll", "System.Core.dll", "MyAssembly.dll" }, "foo.exe", true);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.