罗斯林,传递价值通过hostObject [英] Roslyn, passing values through hostObject

查看:196
本文介绍了罗斯林,传递价值通过hostObject的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过hostObject送一个类,但显然它不想工作:

 使用Roslyn.Compilers ; 
使用Roslyn.Compilers.CSharp;
使用Roslyn.Scripting;
使用Roslyn.Scripting.CSharp;


公共类ShippingService
{
公共类ShippingDetails //我想送
{
公共小数总类{搞定;组; }
公众诠释数量{搞定;组; }
公共字符串目的地{搞定;组; }
}
公共静态字符串ShipingCost(十进制总,诠释数量,串目的地)
{
变种细节=新ShippingDetails
{
总=总,
数量=数量,
目的地=目标
};


{
的ScriptEngine roslynEngine =新的ScriptEngine();
Roslyn.Scripting.Session会话= roslynEngine.CreateSession(详细信息);
session.AddReference(details.GetType()汇编);
session.AddReference(System.Web程序);
session.ImportNamespace(系统);
session.ImportNamespace(System.Web程序);
VAR的结果= session.Execute(details.destination);
返回结果;
}
赶上(例外五)
{
返回e.Message;
}
返回;
}

}

当我调用该函数目的地例如是法国,而我应该得到的结果是这个值,但我得到的错误:




Roslyn.Compilers.CompilationErrorException:( 1,1):错误CS0103:名称'细节'不会在目前的情况下



解决方案
存在

该错误信息是完全正确的。当你有一个主机对象,你不能持有它在你的方法中的局部变量的名称来访问它(怎么会连工作吗?怎么会的ScriptEngine 学习这个名字?)。



相反,你可以访问主机对象的直接的,仿佛你正在写该类型的成员方法(但不完全,则不能使用这个或访问非公共成员)。



这意味着你应该写只是:

  session.Execute(目的地)

BTW,根据净命名规则,公共属性的名称应以大写字母开头(如目标 )。


I am trying to send one class through hostObject but apparently it doesn't want to work:

using Roslyn.Compilers;
using Roslyn.Compilers.CSharp;
using Roslyn.Scripting;
using Roslyn.Scripting.CSharp;


public class ShippingService
{
    public class ShippingDetails//class that I want to send
    {
        public decimal total { get; set; }
        public int quantity { get; set; }
        public string destination { get; set; }
    }
    public static string ShipingCost(decimal total, int quantity, string destination)
    {
        var details = new ShippingDetails
        {
            total = total,
            quantity = quantity,
            destination = destination
        };

        try
            {
                ScriptEngine roslynEngine = new ScriptEngine();
                Roslyn.Scripting.Session session = roslynEngine.CreateSession(details);
                session.AddReference(details.GetType().Assembly);
                session.AddReference("System.Web");
                session.ImportNamespace("System");
                session.ImportNamespace("System.Web");
                var result = session.Execute("details.destination");
                return result;
            }
        catch (Exception e)
            {
            return e.Message;
            }
        return "";
    }   

}

When I am calling the function destination is for example "France", and I should get this value in result but i am getting mistake:

Roslyn.Compilers.CompilationErrorException: (1,1): error CS0103: The name 'details' does not exist in the current context

解决方案

The error message is exactly right. When you have a host object, you can't access it by the name of the local variable that holds it in your method (how would that even work? how would ScriptEngine learn that name?).

Instead, you can access the host object directly, almost as if you were writing a member method of that type (but not quite, you can't use this or access non-public members).

This means that you should write just:

session.Execute("destination")

BTW, according to .Net naming guidelines, names of public properties should start with upper case letter (e.g. Destination).

这篇关于罗斯林,传递价值通过hostObject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆