Skip to content

Use MEF to support dynamic plugins #68

Description

@maraf
public class ManagedExtensibility
{
    private CompositionContainer? container;

    public void Initialize()
    {
        try
        {
            //new DirectoryCatalog(".");

            var catalog = new AggregateCatalog();
            catalog.Catalogs.Add(new AssemblyCatalog(typeof(ManagedExtensibility).Assembly));

            container = new CompositionContainer(catalog);


            var helper = new SnippetProviderHelper();
            container.ComposeParts(helper);
        }
        catch (CompositionException compositionException)
        {
            Console.WriteLine(compositionException.ToString());
        }
    }
}

class SnippetProviderHelper
{
    [ImportMany]
    public IEnumerable<Lazy<ISnippetProvider, IConfigChangeTrackingSnippetProviderMetadata>> Providers { get; set; }
}

public interface IConfigChangeTrackingSnippetProviderMetadata
{
    string ConfigurationKey { get; }
    Type ConfigurationType { get; }
    bool IsNullConfigurationEnabled { get; }
}

[MetadataAttribute]
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
public sealed class ConfigChangeTrackingSnippetProviderAttribute<T> : ExportAttribute, IConfigChangeTrackingSnippetProviderMetadata
{
    public string ConfigurationKey { get; set; }
    public Type ConfigurationType { get; set; }
    public bool IsNullConfigurationEnabled { get; set; }

    public ConfigChangeTrackingSnippetProviderAttribute(string configurationKey, bool isNullConfigurationEnabled = false)
        : base(typeof(ISnippetProvider))
    {
        ConfigurationKey = configurationKey;
        ConfigurationType = typeof(T);
        IsNullConfigurationEnabled = isNullConfigurationEnabled;
    }
}

[ConfigChangeTrackingSnippetProvider<GitHubConfiguration>("GitHub")]
public class TestSnippetProvider : ISnippetProvider
{
    public Task InitializeAsync(SnippetProviderContext context)
    {
        throw new NotImplementedException();
    }

    public Task UpdateAsync(SnippetProviderContext context)
    {
        throw new NotImplementedException();
    }
}
<PackageReference Include="System.ComponentModel.Composition" Version="7.0.0" />

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions