【问题标题】:IronPython attribute is not callableIronPython 属性不可调用
【发布时间】:2023-09-18 08:07:01
【问题描述】:

我对以下 IronPython 属性 (RouteAttribute) 有疑问:

import clr
import System
from System.Web.Http import HttpGetAttribute, ApiController, RouteAttribute


class DemoController(ApiController):

    @RouteAttribute("~/api/test")
    def test(self):
        return "Hallo Welt!"

class DemoControllerClr:

    def getType(self):
        return clr.GetClrType(DemoController)

如果我创建 DemoController-Class 的实例,我总是会收到错误消息:RouteAttribute is not callable。我不会自己创建DemoController 的实例,asp.net web api 2 会尝试这样做。

也许有人对 IronPython 中的属性有一些经验。

编辑

我的目标是混合 C# 和 IronPython API-Controller。我像这样注册控制器(c# 的工作):

controller = new Dictionary<string, System.Web.Http.Controllers.HttpControllerDescriptor>();

controller.Add("General", new System.Web.Http.Controllers.HttpControllerDescriptor(config, "General", typeof(Controller.GeneralController)));

foreach (DBScript script in dbContext.LoadData<DBScript>(optWherePart: "ScriptContentType = 1"))
{ 
    System.Diagnostics.Debugger.Launch();

    var sc = ScriptManager.Singleton.CreateScript(script.ScriptName, ScriptLanguage.IronPython);

    var clrInst = sc.GetClassInstance(script.ScriptName + "Clr");

    controller.Add(script.ScriptName.Replace("Controller", ""), new System.Web.Http.Controllers.HttpControllerDescriptor(config,
        script.ScriptName.Replace("Controller", ""), clrInst.getType()));
}

EDIT2

我正在使用最新版本的 IronPython (2.7.5)。

谢谢!

【问题讨论】:

    标签: c# attributes decorator asp.net-web-api2 ironpython


    【解决方案1】:

    这不起作用,因为依赖注入机制不能与 Ironpython 一起工作,因为 dlr.所以不要工作。

    【讨论】: