Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use MEF to support dynamic plugins #68

Open
maraf opened this issue Apr 11, 2023 · 0 comments
Open

Use MEF to support dynamic plugins #68

maraf opened this issue Apr 11, 2023 · 0 comments
Labels
code enhancement New feature or request

Comments

@maraf
Copy link
Member

maraf commented Apr 11, 2023

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" />
@maraf maraf added enhancement New feature or request code labels Apr 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
code enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant