当前位置: 首页>>代码示例>>C#>>正文


C# Scope.RegisterForDisposal方法代码示例

本文整理汇总了C#中Scope.RegisterForDisposal方法的典型用法代码示例。如果您正苦于以下问题:C# Scope.RegisterForDisposal方法的具体用法?C# Scope.RegisterForDisposal怎么用?C# Scope.RegisterForDisposal使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Scope的用法示例。


在下文中一共展示了Scope.RegisterForDisposal方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Dispose_ListWithOneItem_DisposesItem

        public void Dispose_ListWithOneItem_DisposesItem()
        {
            // Arrange
            var scope = new Scope();

            var disposable = new DisposableObject();

            Assert.IsFalse(disposable.IsDisposedOnce, "Test setup");

            var disposables = new List<IDisposable> { disposable };

            scope.RegisterForDisposal(disposable);

            // Act
            scope.Dispose();

            // Assert
            Assert.IsTrue(disposable.IsDisposedOnce);
        }
开发者ID:jamesqo,项目名称:SimpleInjector,代码行数:19,代码来源:ScopedLifestyleTests.cs

示例2: Dispose_RecursiveResolveTriggeredDuringEndScopeAction_StillDisposesRegisteredDisposables

        public void Dispose_RecursiveResolveTriggeredDuringEndScopeAction_StillDisposesRegisteredDisposables()
        {
            // Arrange
            var scope = new Scope();

            var scopedLifestyle = new FakeScopedLifestyle(scope);

            var container = new Container();

            var disposable = new DisposableObject();

            scope.RegisterForDisposal(disposable);

            container.Register<IPlugin>(() =>
            {
                scope.WhenScopeEnds(() =>
                {
                    container.GetInstance<IPlugin>();
                });

                return new DisposablePlugin();
            });

            container.GetInstance<IPlugin>();

            try
            {
                // Act
                scope.Dispose();

                Assert.Fail("Exception expected.");
            }
            catch
            {
                // Assert
                Assert.IsTrue(disposable.IsDisposedOnce,
                    "Even though there was a recursion detected when executing Action delegates, all " +
                    "registered disposables should still get disposed.");
            }
        }
开发者ID:jamesqo,项目名称:SimpleInjector,代码行数:40,代码来源:ScopedLifestyleTests.cs

示例3: Dispose_RecursiveResolveTriggeredInDispose_ThrowsDescriptiveException

        public void Dispose_RecursiveResolveTriggeredInDispose_ThrowsDescriptiveException()
        {
            // Arrange
            var scope = new Scope();

            var scopedLifestyle = new FakeScopedLifestyle(scope);

            var container = new Container();

            container.Register<IPlugin>(() =>
            {
                var plugin = new DisposablePlugin(disposing: _ =>
                {
                    // Recursive dependency
                    // Although really bad practice, this must not cause an infinit spin or a stackoverflow.
                    container.GetInstance<IPlugin>();
                });

                scope.RegisterForDisposal(plugin);

                return plugin;
            });

            container.GetInstance<IPlugin>();

            // Act
            Action action = () => scope.Dispose();

            // Assert
            AssertThat.ThrowsWithExceptionMessageContains<ActivationException>(@"
                The registered delegate for type IPlugin threw an exception. A recursive registration of 
                Action or IDisposable instances was detected during disposal of the scope. 
                This is possibly caused by a component that is directly or indirectly depending on itself"
                .TrimInside(),
                action);
        }
开发者ID:jamesqo,项目名称:SimpleInjector,代码行数:36,代码来源:ScopedLifestyleTests.cs

示例4: RegisterForDisposal_CalledOnDisposedScope_ThrowsObjectDisposedException

        public void RegisterForDisposal_CalledOnDisposedScope_ThrowsObjectDisposedException()
        {
            // Arrange
            var scope = new Scope();

            scope.Dispose();

            // Act
            Action action = () => scope.RegisterForDisposal(new DisposableObject());

            // Assert
            AssertThat.Throws<ObjectDisposedException>(action,
                "Calling RegisterForDisposal should throw an ObjectDisposedException.");
        }
开发者ID:jamesqo,项目名称:SimpleInjector,代码行数:14,代码来源:ScopedLifestyleTests.cs

示例5: Dispose_WithThrowingAction_StillDisposesInstance

        public void Dispose_WithThrowingAction_StillDisposesInstance()
        {
            // Arrange
            bool disposed = false;

            var scope = new Scope();

            scope.WhenScopeEnds(() =>
            {
                throw new Exception();
            });

            scope.RegisterForDisposal(new DisposableObject(_ =>
            {
                disposed = true;
            }));

            try
            {
                // Act
                scope.Dispose();

                // Assert
                Assert.Fail("Exception expected.");
            }
            catch
            {
                Assert.IsTrue(disposed);
            }
        }
开发者ID:jamesqo,项目名称:SimpleInjector,代码行数:30,代码来源:ScopedLifestyleTests.cs

示例6: Dispose_RegisteredActionAndRegisteredDisposableObject_CallsActionFirst

        public void Dispose_RegisteredActionAndRegisteredDisposableObject_CallsActionFirst()
        {
            // Arrange
            int actionCount = 0;
            int disposeCount = 0;

            var scope = new Scope();

            scope.RegisterForDisposal(new DisposableObject(_ =>
            {
                Assert.AreEqual(2, actionCount);
                disposeCount++;
            }));

            scope.WhenScopeEnds(() =>
            {
                Assert.AreEqual(0, disposeCount);
                actionCount++;
            });

            scope.WhenScopeEnds(() =>
            {
                Assert.AreEqual(0, disposeCount);
                actionCount++;
            });

            scope.RegisterForDisposal(new DisposableObject(_ =>
            {
                Assert.AreEqual(2, actionCount);
                disposeCount++;
            }));

            // Act
            scope.Dispose();

            // Assert
            Assert.AreEqual(2, actionCount);
            Assert.AreEqual(2, disposeCount);
        }
开发者ID:jamesqo,项目名称:SimpleInjector,代码行数:39,代码来源:ScopedLifestyleTests.cs


注:本文中的Scope.RegisterForDisposal方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。