Ejemplo n.º 1
0
    static void Main()
    {
        // importing class instance
        var mcc = new MyClassConsumer();
        // type to export
        var typeToExport = typeof(MyClassA);

        Console.WriteLine("Type to export: {0}", typeToExport);
        var rb = new RegistrationBuilder();

        rb.ForType(typeToExport)
        .ExportInterfaces();
        var catalog   = new AssemblyCatalog(typeof(Program).Assembly, rb);
        var container = new CompositionContainer(catalog);

        container.ComposeParts(mcc);     // bombs if MyClassA's MetadataAttribute is not commented out
        Console.WriteLine("Imported property's type: {0}", mcc.MyClass.GetType());
        Console.ReadLine();
    }
Ejemplo n.º 2
0
    static void Main()
    {
        var catalog         = new AssemblyCatalog(typeof(Program).Assembly);
        var filteredCatalog = catalog.Filter(p =>
        {
            var type = ReflectionModelServices.GetPartType(p).Value;
            return(typeof(IClass).IsAssignableFrom(type) &&                      // implements interface you're looking for
                   Attribute.IsDefined(type, typeof(ExportMetadataAttribute)) && // has ExportMetadata attribute
                   // check for Type == MyClassType.TypeA
                   type.GetCustomAttributes(typeof(ExportMetadataAttribute), true).Any(ca =>
            {
                var ema = (ExportMetadataAttribute)ca;
                return ema.Name == "Type" && (MyClassType)ema.Value == MyClassType.TypeA;
            }));
        });
        var             container = new CompositionContainer(filteredCatalog);
        MyClassConsumer mcc       = new MyClassConsumer();

        container.ComposeParts(mcc);
        Console.WriteLine("Imported property's type: {0}", mcc.MyClass.GetType());
        Console.ReadLine();
    }