小编hel*_*low的帖子

使用Assembly.GetCallingAssembly()不返回调用程序集

在我的ASP.NET MVC应用程序中,我使用一个小助手来遍历所有控制器.这个帮助器位于与我的MVC应用程序不同的程序集中,我正在引用它.

问题是,当在helper中调用Assembly.GetCallingAssembly()方法时,它不会返回MVC app程序集,而是返回帮助程序集.这不是我期望得到的,因为我的所有控制器都存在于MVC app程序集中,我需要反映它.

视图代码(MVC app assembly):

<nav>
   <ul id="menu">
      @foreach(var item in new MvcHelper().GetControllerNames())
      {
         @Html.ActionMenuItem(
              (string)HttpContext.GetGlobalResourceObject("StringsResourse", item), "Index",
              item)
      }
   </ul>
</nav>
Run Code Online (Sandbox Code Playgroud)

帮助程序代码(独立程序集):

public class MvcHelper
{
    public  List<string> GetControllerNames()
    {
        var controllerNames = new List<string>();
        GetSubClasses<Controller>().ForEach(
            type => controllerNames.Add(type.Name));
        return controllerNames;
    }

    private static List<Type> GetSubClasses<T>()
    {
        return Assembly.GetCallingAssembly().GetTypes().Where(
            type => type.IsSubclassOf(typeof(T))).ToList();
    }
}
Run Code Online (Sandbox Code Playgroud)

我在这做错了什么?

c# reflection asp.net-mvc

5
推荐指数
1
解决办法
6456
查看次数

在Arm汇编中使用C中声明的全局变量

我有一个声明全局变量的C文件。该文件将与一些ARM汇编文件一起编译。

int foo;

void call_asm(void);

int main(void) {
    call_asm();
    return foo;
}
Run Code Online (Sandbox Code Playgroud)
call_asm:
    ...
Run Code Online (Sandbox Code Playgroud)

我尝试使用来自arm信息中心链接,但是编译器(arm-linux-gnueabi-gcc)告诉我“ import”是未定义的指令。

我可以简单地做些什么:

LDR    r0, =GLOBAL_VAR
Run Code Online (Sandbox Code Playgroud)

如何在汇编中使用C文件中定义的全局变量?

c assembly arm

5
推荐指数
1
解决办法
8559
查看次数

Bash for 循环和全局扩展

考虑以下 bash 代码:

 for f in /tmp/*.dat; do echo ${f}; done
Run Code Online (Sandbox Code Playgroud)

当我运行它并且输出中没有*.dat文件时/tmp

/tmp/*.dat
Run Code Online (Sandbox Code Playgroud)

这显然不是我想要的。但是,当有这样的文件时,它会打印出正确的

/tmp/foo.dat
Run Code Online (Sandbox Code Playgroud)

当目录中没有这样的文件时,如何强制 for 循环返回“无”。该find-command是不是一种选择,对不起为:/我想也有一个解决方案,而测试中,如果*.dat是文件还是不行。到目前为止有任何解决方案吗?

bash

5
推荐指数
1
解决办法
1567
查看次数

使用graal python从java获取外部环境参数

我在GraalVM中运行Java来使用它来执行python.

Context context = Context.create();
Value v = context.getPolyglotBindings();
v.putMember("arguments", arguments);

final Value result = context.eval("python", contentsOfMyScript);
System.out.println(result);
return jsResult;
Run Code Online (Sandbox Code Playgroud)

问题是python代码应该如何接收"参数".graal文档说明如果这是JS,我会做这样的事情:

const args = Interop.import('arguments');
Run Code Online (Sandbox Code Playgroud)

确实,这很有效.python等价物可能是:

import Interop
args = Interop.import('arguments')

def main():
    return args

main()
Run Code Online (Sandbox Code Playgroud)

这失败了,因为没有这样的模块.我找不到如何从外部语言层获取这些参数的文档,只有pythongraal上的文档以及如何使用python传递给其他东西.

python java graalvm

5
推荐指数
1
解决办法
241
查看次数

跨平台随机再现性

我需要使用随机功能,但也可以在不同的设备(PC/iOS/Android)上重复使用.我正在运行此示例代码,以调整向量:

#include <algorithm>
#include <iostream>
#include <iterator>
#include <random>
#include <vector>

int main() {
    std::mt19937 generator(1337);
    std::cout << "Your seed produced: " << generator() << std::endl;
    std::vector<int> v = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    std::shuffle(v.begin(), v.end(), generator);
    std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, " "));
    std::cout << "\n";
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

两台不同PC(窗口)的输出:

Your seed produced: 1125387415
10 6 8 1 7 2 4 3 5 9
Run Code Online (Sandbox Code Playgroud)

iOS输出:

Your seed produced: 1125387415
9 1 4 6 …
Run Code Online (Sandbox Code Playgroud)

c++ windows random android ios

5
推荐指数
1
解决办法
174
查看次数

如何使用Rust关键字创建一个端点作为查询动态参数?

我使用Rocket库,我需要创建一个端点,其中包含动态参数"type",一个关键字.

我试过这样的东西,但它没有编译:

#[get("/offers?<type>")]
pub fn offers_get(type: String) -> Status {
    unimplemented!()
}
Run Code Online (Sandbox Code Playgroud)

编译器错误:

error: expected argument name, found keyword `type`
Run Code Online (Sandbox Code Playgroud)

是否有可能在火箭中有一个名为"type"的参数?由于我遵循的规范,我无法重命名参数.

rust rust-rocket

5
推荐指数
1
解决办法
126
查看次数

实现proc宏时循环依赖包

我尝试实现Dump类似于serdes的proc_macro Serialize

为了这个目的,我有一个箱子foo包含我的“原始”结构(P1P2在这种情况下),这应该只是dumpable。

接下来,我确实有一个foo_derive包含程序宏本身的板条箱。

因为我想支持多种格式,所以我有第三个板条箱foo_dump,其中包含trait定义Dump(例如,可以转储此结构)和Dumper(这是后端应实现的)。非常直截了当的到这一点。

现在,我想编译它时,出现以下错误:

$ cargo build
error: cyclic package dependency: package `foo v0.1.0 (/tmp/tmp.u34pI5J6qd/example/foo)` depends on itself. Cycle:
package `foo v0.1.0 (/tmp/tmp.u34pI5J6qd/example/foo)`
    ... which is depended on by `foo_dump v0.1.0 (/tmp/tmp.u34pI5J6qd/example/foo_dump)`
    ... which is depended on by `foo_derive v0.1.0 (/tmp/tmp.u34pI5J6qd/example/foo_derive)`
Run Code Online (Sandbox Code Playgroud)

我不知道正确的方法是什么,如何在此板条箱中使用依赖项。我当前的是:

依存关系

这当然是不可能的。

我想念什么?我该怎么做才能打破依赖圈?


mcve @ github

/Cargo.toml

[workspace]
members = [ 
    "foo",
    "foo_derive",
    "foo_dump",
]
Run Code Online (Sandbox Code Playgroud)

/foo/Cargo.toml

[package]
name …
Run Code Online (Sandbox Code Playgroud)

rust rust-macros rust-proc-macros

5
推荐指数
1
解决办法
202
查看次数

在多个系统运行时关闭 actix

我的应用程序基于使用 actix 和 actix-web 的库 (Library-A)。我正在添加第二个库(Library-B),它运行一个 http 服务器,也使用 actix-web。actix::system我为此使用了一个单独的线程。收到 SIGINT 后,仅 Library-B actix 系统关闭,而 Library-A 继续运行。后续 SIGINT 不会关闭正在运行的 actix 系统。

正常关闭两个正在运行的 actix 系统的正确方法是什么?

Library-B 的代码,用于启动新的 actix 系统并运行 http 服务器:

thread::spawn(move || {
    let sys = actix::System::new("monitor");
    server::new(|| App::new()
        .route("/metrics", http::Method::GET, endpoint))
        .bind(format!("0.0.0.0:{}", port))
        .unwrap()
        .start();
    sys.run();
    println!("Closing monitor actix system");
    // --- SIGINT gets me here... how do I shut down gracefully?
});
Run Code Online (Sandbox Code Playgroud)

我为独立图书馆启动一个新系统是否正确?如何优雅地关闭?

rust rust-actix

5
推荐指数
1
解决办法
2658
查看次数

如何在 Rust 中静态链接 Node.js?

我想将node.js 嵌入Rust 中。我对使用 NAPI 编写 Node.js 插件或从 Rust 内部广泛控制 Node.js不感兴趣。我需要的只是 node.js main()start 方法 - 相当于执行node myscript.js.

为什么?我正在用 Rust 构建一个独立的单文件二进制桌面应用程序,并希望使用嵌入的 node.js 运行时运行 node.js 脚本。Node.js 不保证位于最终用户的计算机上,并且启动时间很敏感,因此不希望将 Node.js 的独立 zip 从二进制文件中提取到文件系统。

我相信我在 rust(二进制)项目中静态链接 node.js 时遇到问题。

我拉下nodejs源代码

git clone https://github.com/nodejs/node
Run Code Online (Sandbox Code Playgroud)

并将main方法重命名为node_main.cc

sed -i .bak "s/int main(/int node_main(/g" ./src/node_main.cc
Run Code Online (Sandbox Code Playgroud)

然后我将 node.js 构建为静态库

./configure --enable-static
make -j4
Run Code Online (Sandbox Code Playgroud)

我有一个 C++ 包装文件来通过以下方式wrapper.cpp公开该方法node_main()extern c

包装器.cpp:

#include <string>
#include <iostream>

using namespace std;

int node_main(int argc, char **argv);

extern "C" …
Run Code Online (Sandbox Code Playgroud)

c++ static-linking node.js rust

5
推荐指数
1
解决办法
1286
查看次数

Trait::&lt;T&gt; 和 &lt;Trait&lt;T&gt;&gt; 有什么区别?

我有一个类型,我可以通过它的方法访问

SomeTrait::<T>::method()
Run Code Online (Sandbox Code Playgroud)

但我不明白这和

<SomeTrait<T>>::method()
Run Code Online (Sandbox Code Playgroud)

在 C++ 中,我希望这样:

SomeTrait<T>::method()
Run Code Online (Sandbox Code Playgroud)

这两个有区别吗?他们似乎都在调用on的<T>专业化。methodSomeTrait

generics rust

5
推荐指数
1
解决办法
104
查看次数