我试图通过 hostObject 发送一个类,但显然它不想工作:
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 "";
}
}
当我调用函数时,目标是例如“France”,我应该在结果中得到这个值,但我出错了:
Roslyn.Compilers.CompilationErrorException: (1,1): 错误 CS0103: 当前上下文中不存在名称“详细信息”