Exemplo n.º 1
0
        public void AnyPart_DisposableRecomposabeImport_ShouldNotBeCollected()
        {
            var catalog   = new TypeCatalog(typeof(AnyPartDisposableRecomposable));
            var container = new CompositionContainer(catalog);

            // Setup dependency
            CompositionBatch batch = new CompositionBatch();
            var valueKey           = batch.AddExportedValue("Value", 21);

            container.Compose(batch);
            batch = null;

            var refTracker = new ReferenceTracker();

            refTracker.AddReferencesNotExpectedToBeCollected(
                container.GetExportedValue <AnyPartDisposableRecomposable>());

            refTracker.CollectAndAssert();

            // Lets make sure recomposition doesn't blow anything up here.
            batch = new CompositionBatch();
            batch.RemovePart(valueKey);
            batch.AddExportedValue("Value", 42);
            container.Compose(batch);
            batch = null;

            var exportedValue = (AnyPartDisposableRecomposable)refTracker.ReferencesNotExpectedToBeCollected[0].Target;

            Assert.Equal(42, exportedValue.Value);

            GC.KeepAlive(container);

            container.Dispose();

            Assert.True(exportedValue.IsDisposed, "Any parts should be disposed with the container!");
        }