Roslyn VisualBasic.ScriptEngine doesnt recognize hostObject written on C#

人走茶凉 提交于 2019-12-05 14:50:26

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.

I accepted the previous answer because it really gave me an idea how to make VB.NET script work, however HostObject still doesnt work

So actual workaround consists of 2 steps

1) Use Imports ScriptModel in VB code

var vbCode = @"Imports ScriptModel

    If (Row.Code = 12) Then 
        Row.MappedCode = 1
    End If";

2) Do not use HostObject. Define Row as public static class

namespace ScriptModel
{
    public static class Row
    {
        public static int Code { get; set; }
        public static int MappedCode { get; set; }
    }
}

I believe that answer on MS forum is also correct, http://social.msdn.microsoft.com/Forums/en-US/roslyn/thread/89970f0b-1c1c-47da-a180-9c4710abc4b9 in current version HostObject is not supported for VB, but I hope it will be supported in next version

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!