[英]How to compile and run C# code at runtime where the compiled code can access the rest of the program
I have a C# game, I wish to allow our scripter to write small code files such as what is below so we can patch and mod some of our systems. 我有一个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); }
}
}
As you can see the file uses types and methods from the game, is this possible or does any runtime created code need to be confined to itself? 正如您所看到的,该文件使用游戏中的类型和方法,这是可能的,还是任何运行时创建的代码都需要限制在自身?
Bonus if it also works on the 360. 奖金,如果它也适用于360。
Edit: Paint.net has a code lab plugin that does exactly this, so I know it must be possible. 编辑:Paint.net有一个代码实验室插件,正是这样做,所以我知道它必须是可能的。
Is it possible to dynamically compile and execute C# code fragments? 是否可以动态编译和执行C#代码片段? deals with the same thing.
处理同样的事情。 Being able to reference your classes just means that one of the assemblies you want to reference should be the one with
Creature
in it. 能够引用您的类只意味着您要引用的其中一个程序集应该是包含
Creature
的程序集。 Eg 例如
var parameters = new CompilerParameters(
new[] { "mscorlib.dll", "System.Core.dll", "MyAssembly.dll" }, "foo.exe", true);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.