context: // -----------------------------------------------------------------------
  // Copyright (c) Microsoft Corporation.  All rights reserved.
  // -----------------------------------------------------------------------
  using System;
  using System.Linq;
  using System.ComponentModel.Composition.Factories;
  using Microsoft.VisualStudio.TestTools.UnitTesting;
  using System.UnitTesting;
  using System.ComponentModel.Composition.Hosting;
  using System.ComponentModel.Composition.UnitTesting;
  
  namespace System.ComponentModel.Composition.AttributedModel
  {
      [TestClass]
      public class AttributedModelCompositionTests
      {
          [TestMethod]
          public void PartContainingOnlyStaticExports_ShouldNotCauseInstanceToBeCreated()
          {  
              var container = ContainerFactory.CreateWithAttributedCatalog(typeof(HasOnlyStaticExports));
              
              Assert.AreEqual("Field", container.GetExportedValue<string>("Field"));
              Assert.AreEqual("Property", container.GetExportedValue<string>("Property"));
              Assert.IsNotNull("Method", container.GetExportedValue<Func<string>>("Method")());
  
              Assert.IsFalse(HasOnlyStaticExports.InstanceCreated);
          }
  
          [TestMethod]
          public void ExportOnAbstractBase_DoesNotReturnNull()
          {   // 499393 - Classes inheriting from an exported 
              // abstract class are exported as 'null'
  
              var container = ContainerFactory.Create();
              CompositionBatch batch = new CompositionBatch();
  
              var definition = PartDefinitionFactory.CreateAttributed(typeof(Derived));
              batch.AddPart(definition.CreatePart());
              container.Compose(batch);
  
              Assert.IsNotNull(container.GetExportedValueOrDefault<Base>());
          }
  
          [TestMethod]
          public void ReadOnlyFieldImport_ShouldThrowComposition()
          {
              var importer = PartFactory.CreateAttributed(new ReadOnlyPropertyImport());
              var container = ContainerFactory.Create();
              CompositionBatch batch = new CompositionBatch();
              batch.AddPart(importer);
              batch.AddExportedValue("readonly", "new value");
  
              CompositionAssert.ThrowsError(ErrorId.ImportEngine_PartCannotActivate,
                                            ErrorId.ReflectionModel_ImportNotWritable,
                                            RetryMode.DoNotRetry, () =>
              {
                  container.Compose(batch);
              });
          }
  
          [TestMethod]
          public void ReadOnlyPropertyImport_ShouldThrowComposition()
          {
              var importer = PartFactory.CreateAttributed(new ReadOnlyPropertyImport());
              var container = ContainerFactory.Create();
              CompositionBatch batch = new CompositionBatch();
              batch.AddPart(importer);
              batch.AddExportedValue("readonly", "new value");
  
              CompositionAssert.ThrowsError(ErrorId.ImportEngine_PartCannotActivate,
                                            ErrorId.ReflectionModel_ImportNotWritable, RetryMode.DoNotRetry, () =>
              {
                  container.Compose(batch);
              });
          }
  
          [TestMethod]
          public void WriteOnlyPropertyExport_ShouldThrowComposition()
          {
              var importer = PartFactory.CreateAttributed(new WriteOnlyPropertyExport());
              var container = ContainerFactory.Create();
              CompositionBatch batch = new CompositionBatch();
              batch.AddPart(importer);
              container.Compose(batch);
  
              CompositionAssert.ThrowsError(ErrorId.ImportEngine_PartCannotGetExportedValue, ErrorId.ReflectionModel_ExportNotReadable, () =>
              {
                  container.GetExportedValue<string>("writeonly");
              });
          }
  
          [TestMethod]
          public void ImportValueMismatchFromInt32ToDateTime()
          {
              var container = ContainerFactory.Create();
              CompositionBatch batch = new CompositionBatch();
              batch.AddPart(new ImportValueTypes());
              batch.AddExportedValue("DateTime", typeof(DateTime), 42);
  
              CompositionAssert.ThrowsError(ErrorId.ImportEngine_PartCannotSetImport,
                                            ErrorId.ReflectionModel_ImportNotAssignableFromExport,
                                            RetryMode.DoNotRetry, () =>
              {
                  container.Compose(batch);
              });
          }
  
          [TestMethod]
          public void ImportValueMismatchFromInt16ToInt32()
          {
              var container = ContainerFactory.Create();
              CompositionBatch batch = new CompositionBatch();
  
              batch.AddPart(new ImportValueTypes());
              batch.AddExportedValue("Int32", typeof(Int32), (short)10);
  
              CompositionAssert.ThrowsError(ErrorId.ImportEngine_PartCannotSetImport,
                                ErrorId.ReflectionModel_ImportNotAssignableFromExport,
                                RetryMode.DoNotRetry, () =>
              {
                  container.Compose(batch);
              });
  
          }
  
          [TestMethod]
          public void ImportValueMismatchFromInt32ToInt16()
          {
              var container = ContainerFactory.Create();
              CompositionBatch batch = new CompositionBatch();
  
              batch.AddPart(new ImportValueTypes());
              batch.AddExportedValue("Int16", typeof(Int16), (int)10);
  
              CompositionAssert.ThrowsError(ErrorId.ImportEngine_PartCannotSetImport,
                                            ErrorId.ReflectionModel_ImportNotAssignableFromExport,
                                            RetryMode.DoNotRetry, () =>
              {
                  container.Compose(batch);
              });
          }
  
          [TestMethod]
          public void ImportValueMismatchFromInt32ToUInt32()
          {
              var container = ContainerFactory.Create();
              CompositionBatch batch = new CompositionBatch();
  
              batch.AddPart(new ImportValueTypes());
              batch.AddExportedValue("UInt32", typeof(UInt32), (int)10);
  
              CompositionAssert.ThrowsError(ErrorId.ImportEngine_PartCannotSetImport,
                                            ErrorId.ReflectionModel_ImportNotAssignableFromExport,
                                            RetryMode.DoNotRetry, () =>
              {
                  container.Compose(batch);
              });
          }
  
          [TestMethod]
          public void ImportValueMismatchFromUInt32ToInt32()
          {
              var container = ContainerFactory.Create();
              CompositionBatch batch = new CompositionBatch();
  
              batch.AddPart(new ImportValueTypes());
              batch.AddExportedValue("Int32", typeof(Int32), (uint)10);
  
              CompositionAssert.ThrowsError(ErrorId.ImportEngine_PartCannotSetImport,
                                            ErrorId.ReflectionModel_ImportNotAssignableFromExport,
                                            RetryMode.DoNotRetry, () =>
              {
                  container.Compose(batch);
              });
          }
  
          [TestMethod]
          public void ImportValueMismatchFromInt32ToInt64()
          {
              var container = ContainerFactory.Create();
              CompositionBatch batch = new CompositionBatch();
  
              batch.AddPart(new ImportValueTypes());
              batch.AddExportedValue("Int64", typeof(Int64), (int)10);
  
              CompositionAssert.ThrowsError(ErrorId.ImportEngine_PartCannotSetImport,
                                            ErrorId.ReflectionModel_ImportNotAssignableFromExport,
                                            RetryMode.DoNotRetry, () =>
              {
                  container.Compose(batch);
              });
          }
  
          [TestMethod]
          public void ImportValueMismatchFromSingleToDouble()
          {
              var container = ContainerFactory.Create();
              CompositionBatch batch = new CompositionBatch();
  
              batch.AddPart(new ImportValueTypes());
              batch.AddExportedValue("Double", typeof(Double), (float)10);
  
              CompositionAssert.ThrowsError(ErrorId.ImportEngine_PartCannotSetImport,
                                            ErrorId.ReflectionModel_ImportNotAssignableFromExport,
                                            RetryMode.DoNotRetry, () =>
              {
                  container.Compose(batch);
              });
          }
  
          [TestMethod]
          public void ImportValueMismatchFromDoubleToSingle()
          {
              var container = ContainerFactory.Create(); 
              CompositionBatch batch = new CompositionBatch();
  
              batch.AddPart(new ImportValueTypes());
              batch.AddExportedValue("Single", typeof(Single), (double)10);
  
              CompositionAssert.ThrowsError(ErrorId.ImportEngine_PartCannotSetImport,
                                            ErrorId.ReflectionModel_ImportNotAssignableFromExport,
                                            RetryMode.DoNotRetry, () =>
              {
                  container.Compose(batch);
              });
          }
  
          [TestMethod]
          public void ImportValueMismatchFromSingleToInt32()
          {
              var container = ContainerFactory.Create();
              CompositionBatch batch = new CompositionBatch();
  
              batch.AddPart(new ImportValueTypes());
              batch.AddExportedValue("Int32", typeof(Int32), (float)10);
  
              CompositionAssert.ThrowsError(ErrorId.ImportEngine_PartCannotSetImport,
                                            ErrorId.ReflectionModel_ImportNotAssignableFromExport,
                                            RetryMode.DoNotRetry, () =>
              {
                  container.Compose(batch);
              });
          }
  
          [TestMethod]
          public void ImportValueMismatchFromInt32ToSingle()
          {
              var container = ContainerFactory.Create();
              CompositionBatch batch = new CompositionBatch();
  
              batch.AddPart(new ImportValueTypes());
              batch.AddExportedValue("Single", typeof(Single), (int)10);
  
              CompositionAssert.ThrowsError(ErrorId.ImportEngine_PartCannotSetImport,
                                            ErrorId.ReflectionModel_ImportNotAssignableFromExport,
                                            RetryMode.DoNotRetry, () =>
              {
                  container.Compose(batch);
              });
          }
  
          [TestMethod]
          public void MemberExports()
          {
              var exporter = PartFactory.CreateAttributed(new ObjectWithMemberExports());
              var container = ContainerFactory.Create();
              CompositionBatch batch = new CompositionBatch();
              batch.AddPart(exporter);
              container.Compose(batch);
  
              var filedExports = container.GetExports<int>("field");
              ExportsAssert.AreEqual(filedExports, 1, 5);
  
              var readonlyExports = container.GetExports<int>("readonly");
              ExportsAssert.AreEqual(readonlyExports, 2, 6);
  
  
gt:             var propertyExports = container.GetExports<int>("property");