罗斯林VisualBasic.ScriptEngine犯规承认hostObject写在C# [英] Roslyn VisualBasic.ScriptEngine doesnt recognize hostObject written on C#
问题描述
我们的项目需要有一个简单的业务规则的能力,我们的客户可以在Visual Basic脚本。虽然我们的主要项目是写在C#
哪些客户要EXECUT可能是这样的(我正在考虑的simpliest可能的情况下)的剧本
VAR vbCode = @
。如果(Row.Code = 12),然后
Row.MappedCode = 1
端如果
因此,我创建在C#中RowData类代码和MappedCode性能
命名空间ScriptModel
{
公共类RowData
{
公众诠释代码{搞定;组; }
公众诠释MappedCode {搞定;组; }
}
}
我创建了一个简单的主机对象类像
命名空间ScriptModel
{
公共类HostObjectModel
{
公共RowData行{得到;组; }
}
}
使用Roslyn.Scripting.VisualBasic .ScriptEngine我创建一个引擎,
创建一个会话与HostObjectModel的实例,并进行engine.Execute(vbCode,会话)
VAR hostObj =新HostObjectModel();
hostObj.Row =新RowData();
hostObj.Row.Code = 12;
变种引擎=新Roslyn.Scripting.VisualBasic.ScriptEngine(
新议会[] {hostObj.GetType()。大会},
新的字符串[] {ScriptModel });
VAR会话= Session.Create(hostObj);
engine.Execute(vbCode,会话);
和它告诉我,
(2,25):错误BC30451:'行'未声明。这可能是人迹罕至的
由于其保护级别。
块引用>
但是,如果我在C#创建类似的代码段
<如果pre>VAR csharpCode = @
(Row.Code == 12)
{
行。 MappedCode = 1;
};;
和使用CSharp.ScriptEngine这一切都会正常工作
那么,什么是一个问题,为什么VisualBasic.ScriptEngine无法看到这是在C#编译的类,它应该是,我认为,基于相同的MSIL语言或我错了的公共属性?
更新:我在VB安装Visual Basic和创建ScriptModel库。我也换成ROW()函数行物业无论是在类的声明,并在vbCode。没有帮助。 :(似乎VisualBasic.ScriptEngine不工作在所有的时候我从C#中运行它。
解决方案有关VB脚本,我发现你有包括在脚本的开头如下:
进口ScriptModel
我猜你会自动预挂起上面的代码字符串,因此用户不需要记住,包括它。
我一直没能得到,当作为的ScriptEngine创建的一部分添加它的工作也似乎不使用事后工作:
engine.ImportedNamespaces.Append(ScriptModel);
这是尽管事后ImportedNamespaces计数为1。用c#你似乎并不需要在所有导入的命名空间。
Our project need ability to have a simple business rules our customers can script in Visual basic. While our main program is written on C#
The script which customers want to execut could be like this (I am considering the simpliest possible case)
var vbCode = @" If (Row.Code = 12) Then Row.MappedCode = 1 End If";
So I created a RowData class in C# with Code and MappedCode properties
namespace ScriptModel { public class RowData { public int Code { get; set; } public int MappedCode { get; set; } } }
I created a simple host object class like
namespace ScriptModel { public class HostObjectModel { public RowData Row { get; set; } } }
Using Roslyn.Scripting.VisualBasic.ScriptEngine I create an engine, create a session with an instance of HostObjectModel and perform engine.Execute(vbCode, session)
var hostObj = new HostObjectModel(); hostObj.Row = new RowData(); hostObj.Row.Code = 12; var engine = new Roslyn.Scripting.VisualBasic.ScriptEngine( new Assembly[] {hostObj.GetType().Assembly}, new string[] {"ScriptModel"} ); var session = Session.Create(hostObj); engine.Execute(vbCode , session);
And it tells me that
(2,25): error BC30451: 'Row' is not declared. It may be inaccessible due to its protection level.
But if I create the similar code snippet on C#
var csharpCode = @" if (Row.Code == 12) { Row.MappedCode = 1; };";
and use CSharp.ScriptEngine it all will work correctly
So, what is a problem, why VisualBasic.ScriptEngine not able to see public properties of the class which was compiled in C#, it should be, I think, based on the same MSIL language or I am wrong?
Update: I installed Visual Basic and created ScriptModel library on VB. I also replaced Row property with Row() function both in class declaration and in vbCode. Neither helped. :( seems that VisualBasic.ScriptEngine doesnt work at all when I run it from C#.
解决方案For VB scripting I found you have to include the following at the start of the script:
Imports ScriptModel
I guess you could automatically pre-pend the above to the code string so your users don't need to remember to include it.
I have not been able to get it to work when added as part of the ScriptEngine creation. It also doesn't seem to work afterwards by using:
engine.ImportedNamespaces.Append("ScriptModel");
This is despite the fact that afterwards the ImportedNamespaces count is 1. With c# you don't seem to need to import the namespace at all.
这篇关于罗斯林VisualBasic.ScriptEngine犯规承认hostObject写在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!