简体   繁体   English

如何在运行时编译和运行C#代码,其中编译的代码可以访问程序的其余部分

[英]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.

相关问题 如何在运行时编译的 C# 代码中使用“UnityEngine”? - How to use “UnityEngine” in runtime-compiled C# code? 如何从C#代码运行Cplex程序? - How can I run cplex program from c# code? 是在编写 C# 代码时还是在编译程序时生成了编译时错误? - Is a compile-time error generated as C# code is being written or when the program is being compiled? 以原生速度运行动态编译的C#代码......怎么样? - Run dynamically compiled C# code at native speed… how? 在运行时编译C#代码扩展 - Compile C# code extension at runtime 在运行时编译C#数组并在代码中使用它? - Compile a C# Array at runtime and use it in code? 如何在主C#代码和运行时编译代码中创建和使用相同的类? - How to create and use same class in main C# code and runtime compiled code? 如何将以下代码转换为C#中的编译表达式? - How can I convert the following code to a Compiled Expression in C#? C# Roslyn 编译 - 在动态编译的代码中使用 Nuget 包 - 编译但在运行时抛出异常(无法加载文件或程序集) - C# Roslyn Compile - Use Nuget Package within dynamically compiled Code - compiles but throws an Exception at runtime (Could not load file or assembly) 将类类型传递给运行时编译的代码C# - passing class types to runtime compiled code C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM