C#和CS脚本:不在上下文中的变量
[C#新手] 嗨。这是对CS脚本3.28.7的测试,将脚本添加到C#。 我需要实现非常简单的函数,这些函数稍后将从cfg文件中读取 我浏览了文档,但没有找到阅读外部类和静态变量的方法。对于C#和CS脚本:不在上下文中的变量,c#,cs-script,C#,Cs Script,[C#新手] 嗨。这是对CS脚本3.28.7的测试,将脚本添加到C#。 我需要实现非常简单的函数,这些函数稍后将从cfg文件中读取 我浏览了文档,但没有找到阅读外部类和静态变量的方法。对于值和rnd我都得到消息名称XXX在此上下文中不可用 我忘了什么 using System; using CSScriptLibrary; namespace EmbedCS { class Program { public static int[] values = { 1,
值
和rnd
我都得到消息名称XXX在此上下文中不可用
我忘了什么
using System;
using CSScriptLibrary;
namespace EmbedCS
{
class Program
{
public static int[] values = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
static Random rnd = new Random();
static void Main(string[] args)
{
ExecuteTest();
Console.Read();
}
private static void ExecuteTest()
{
bool result;
var scriptFunction = CSScript.CreateFunc<bool>(@"
bool func() {
int a = rnd.Next(10);
int b = rnd.Next(10);
return values[a] > values[b];
}
");
result = (bool)scriptFunction();
Console.Read();
}
}
}
使用系统;
使用csscript库;
命名空间嵌入
{
班级计划
{
公共静态int[]值={1,2,3,4,5,6,7,8,9,10};
静态随机rnd=新随机();
静态void Main(字符串[]参数)
{
ExecuteTest();
Console.Read();
}
私有静态void ExecuteTest()
{
布尔结果;
var scriptFunction=CSScript.CreateFunc(@)
布尔函数(){
int a=下一个(10)的rnd;
int b=下一个(10)的rnd;
返回值[a]>值[b];
}
");
结果=(bool)scriptFunction();
Console.Read();
}
}
}
这个应该可以用
using System;
using CSScriptLibrary;
namespace EmbedCS
{
public class Program
{
public static int[] values = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
public static Random rnd = new Random();
static void Main(string[] args)
{
ExecuteTest();
Console.Read();
}
private static void ExecuteTest()
{
bool result;
var scriptFunction = CSScript.CreateFunc<bool>(@"
bool func() {
int a = EmbedCS.Program.rnd.Next(10);
int b = EmbedCS.Program.rnd.Next(10);
return EmbedCS.Program.values[a] > EmbedCS.Program.values[b];
}
");
result = (bool)scriptFunction();
Console.Read();
}
}
}
使用系统;
使用csscript库;
命名空间嵌入
{
公共课程
{
公共静态int[]值={1,2,3,4,5,6,7,8,9,10};
公共静态随机rnd=新随机();
静态void Main(字符串[]参数)
{
ExecuteTest();
Console.Read();
}
私有静态void ExecuteTest()
{
布尔结果;
var scriptFunction=CSScript.CreateFunc(@)
布尔函数(){
INTA=EmbedCS.Program.rnd.Next(10);
intb=EmbedCS.Program.rnd.Next(10);
返回EmbedCS.Program.values[a]>EmbedCS.Program.values[b];
}
");
结果=(bool)scriptFunction();
Console.Read();
}
}
}
记住,在C#中,一切都是隐含的
您的func()
不是程序的成员。因此他们无法识别程序中的字段
有些动态语言在语言级别上有绑定上下文(比如ruby的绑定
),所以库可以执行一些黑魔法。但不是在C#中。如果你想让代码干净,试试这个。我查看了链接,但不知道在我的代码中使用static
将放在哪里。我会在脚本中使用它作为第一行(使用静态EmbedCS.Program.values;
),但找不到正确的语法。