小编Ian*_*vis的帖子

如何自动测试中等信任代码

我想编写以中等信任方式运行的自动化测试,如果需要完全信任,则会失败.

我正在编写一个库,其中某些功能仅在完全信任的情况下可用,并且我想验证我希望以中等信任运行的代码将正常工作.如果还想知道如果我改变一个需要完全信任的类,我的测试将失败.

我已经尝试创建另一个AppDomain并加载中等信任PolicyLevel,但我总是得到程序集错误或在尝试运行交叉AppDomain回调时无法加载其依赖项.

有没有办法解决这个问题?

更新:基于回复,这是我的.请注意,正在测试的类必须扩展MarshalByRefObject.这是非常有限的,但我没有看到解决方法.

using System;
using System.Reflection;
using System.Security;
using System.Security.Permissions;
using Xunit;

namespace PartialTrustTest
{
    [Serializable]
    public class ClassUnderTest : MarshalByRefObject
    {
        public void PartialTrustSuccess()
        {
            Console.WriteLine( "partial trust success #1" );
        }

        public void PartialTrustFailure()
        {
            FieldInfo fi = typeof (Int32).GetField( "m_value", BindingFlags.Instance | BindingFlags.NonPublic );
            object value = fi.GetValue( 1 );
            Console.WriteLine( "value: {0}", value );
        }
    }

    public class Test
    {
        [Fact]
        public void MediumTrustWithExternalClass()
        {
            // ClassUnderTest must extend MarshalByRefObject
            var classUnderTest …
Run Code Online (Sandbox Code Playgroud)

c# automated-tests medium-trust full-trust functional-testing

13
推荐指数
2
解决办法
1711
查看次数

什么是PowerShell ScriptBlock

PowerShell ScriptBlock不是词法闭包,因为它不会关闭其声明环境中引用的变量.相反,似乎利用动态范围和自由变量,这些变量在运行时绑定在lambda表达式中.

function Get-Block {
  $b = "PowerShell"
  $value = {"Hello $b"}
  return $value
}
$block = Get-Block
& $block
# Hello
# PowerShell is not written as it is not defined in the scope
# in which the block was executed.


function foo {
  $value = 5
  function bar {
    return $value
  }
  return bar
}
foo
# 5
# 5 is written $value existed during the evaluation of the bar function
# it is my understanding …
Run Code Online (Sandbox Code Playgroud)

powershell lambda closures scope function

11
推荐指数
2
解决办法
1万
查看次数

如果Ninject尚未绑定,如何在Ninject中绑定它?

是否可以将Ninject配置为在绑定时不绑定依赖项.

例如

如果我们加载一个名为Client1的模块,其中包含:

public class Client1Module:NinjectModule
{
    public override void Load()
    {
         Bind<IService>.To<FancyService>()
    }
}
Run Code Online (Sandbox Code Playgroud)

然后我们加载一个名为Base contains的模块

public class BaseModule:NinjectModule
{
    public override void Load()
    {
          Bind<IService>.To<BasicService>()
    }
}
Run Code Online (Sandbox Code Playgroud)

我们希望确保BasicService不受约束,系统始终使用FancyService.我们不会在设计时知道FancyService是否存在.如果找到Client1模块,则会加载它.

我并不是真的想在每次注射等时都有一堆重复的锅炉板代码.因为有50-60个依赖项,所有这些都可以在客户端模块中进行更改.

有任何想法吗?

asp.net dependency-injection ninject

8
推荐指数
2
解决办法
2268
查看次数