代码之家  ›  专栏  ›  技术社区  ›  M.Y. Babt

滤饼:不安全加载组件

  •  1
  • M.Y. Babt  · 技术社区  · 8 年前

    我用蛋糕0.21.1.0。

    我想添加一个执行与此PowerShell操作等效的任务:

    $dlls = @(${dll1}, ${dll2})
    $dlls | % { [Reflection.Assembly]::UnsafeLoadFrom($_) }
    [StaticClassFromLocalLibrary]::SomeStaticMethod(SomeArgument)
    

    我决定不使用 the CAKE PowerShell add-in <script>.ps1 is not digitally signed. The script will not execute on the system . 由于我雇主的IT政策,我不允许参加 Set-ExecutionPolicy

    System.Reflection.Assembly.UnsafeLoadFrom(dll1);
    System.Reflection.Assembly.UnsafeLoadFrom(dll2);
    StaticClassFromLocalLibrary.SomeStaticMethod(SomeArgument);  
    

    但是,引发了一个异常,表示名称 StaticClassFromLocalLibrary 在当前上下文中找不到。

    然后,我在我的蛋糕脚本中添加了这一行:

    #r @"\\hostName\Folder1\Folder2\Folder3\Folder4\Some.Local.Project.dll"
    

    #r 指令(或任何其他CAKE命令)来指定我希望以不安全的方式加载DLL?

    this page 并在接受的答案中采纳建议。

    1 回复  |  直到 8 年前
        1
  •  2
  •   devlead    8 年前

    Cake中没有内置的不安全加载,但因为它只是C语言,所以您可以将PowerShell代码片段转换为C语言#

    var assembly = System.Reflection.Assembly.UnsafeLoadFrom("./tools/Addins/Newtonsoft.Json/lib/net45/Newtonsoft.Json.dll");
    
    var type = assembly.GetType("Newtonsoft.Json.JsonConvert");
    
    var method = type.GetMethod("SerializeObject", new [] {typeof(object) });
    
    var SerializeObject = (Func<object, string>) Delegate.CreateDelegate(
                                               typeof(Func<object, string>), null, method);
    
    var json = SerializeObject(new { Hello = "World"});
    
    Information("{0}", json);
    

    输出如下内容

    {"Hello":"World"}