GithubHelp home page GithubHelp logo

console-plugin-example's Introduction

Console - Plugin Manager

Eklentileri yüklemek için özel bir AssemblyLoadContext'in nasıl oluşturulacağını gösteren ufak bir projedir. Eklentinin bağımlılıklarını çözmek için bir AssemblyDependencyResolver kullanılır. Eklentinin bağımlılıklarını barındırma uygulamasından doğru şekilde izole eder.

Göz attığım microsoft dökümanı

Özellikler

Ana projede (AppWithPlugin) eklenmiş olan eklenti projeleri (HelloPlugin ve GithubPlugin) bağımlılık olarak eklemeden çalışmasını sağlayan bir çözüm oluşturun. Bu, ana proje ile eklenti projeleri arasındaki bağımlılığı ortadan kaldırarak projeler arası esneklik ve bağımsızlık kazandırır.

AppWithPlugin/Program.cs - LoadPlugin Metodu

Aynı çözüm altında oluşan farklı projeler olarak düşündüğüm için çözümün path'ini kök dizin string root olarak aldım.

static Assembly LoadPlugin(string relativePath)
{
    
    string root = Path.GetFullPath(Path.Combine(
        Path.GetDirectoryName(
            Path.GetDirectoryName(
                Path.GetDirectoryName(
                    Path.GetDirectoryName(
                        Path.GetDirectoryName(typeof(Program).Assembly.Location)))))));

    string pluginLocation = Path.GetFullPath(Path.Combine(root, relativePath.Replace('\\', Path.DirectorySeparatorChar)));

    PluginLoadContext loadContext = new PluginLoadContext(pluginLocation);

    return loadContext.LoadFromAssemblyName(new AssemblyName(Path.GetFileNameWithoutExtension(pluginLocation)));
}

AppWithPlugin/Program.cs - CreateCommands Metodu

ICommand interface'inden türeyen sınıfları bulur.

static IEnumerable<ICommand> CreateCommands(Assembly assembly)
{
    int count = 0;

    foreach (Type type in assembly.GetTypes())
    {
        if (typeof(ICommand).IsAssignableFrom(type))
        {
            ICommand result = Activator.CreateInstance(type) as ICommand;
            if (result != null)
            {
                count++;
                yield return result;
            }
        }
    }

    if (count == 0)
    {
        string availableTypes = string.Join(",", assembly.GetTypes().Select(t => t.FullName));
        throw new ApplicationException(
            $"Can't find any type which implements ICommand in {assembly} from {assembly.Location}.\n" +
            $"Available types: {availableTypes}");
    }
}

Important

Plugin olarak hazırlanan projelerde csproj dosyasında düzenlemeler yapılmalıdır.

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <EnableDynamicLoading>true</EnableDynamicLoading>
  </PropertyGroup>

  <ItemGroup>
	  <ProjectReference Include="..\PluginBase\PluginBase.csproj">
		  <Private>false</Private>
		  <ExcludeAssets>runtime</ExcludeAssets>
	  </ProjectReference>
  </ItemGroup>

</Project>
  • EnableDynamicLoading → Projeyi eklenti olarak kullanılabilecek şekilde hazırlar . Diğer şeylerin yanı sıra bu, tüm bağımlılıklarını projenin çıktısına kopyalayacaktır.
  • Private → Önemlidir. Bu, MSBuild'e PluginBase.dll dosyasını HelloPlugin'in çıkış dizinine kopyalamamasını söyler.
  • ExcludeAssets → Önemlidir. PluginBaseBu ayar aynı etkiye sahiptir ancak projenin veya bağımlılıklarından birinin içerebileceği falsepaket referansları üzerinde çalışır.

Kullanım

  • Pluginler eklenir. (Pathleri verilerek eklenir.)
  • Listelemede eklenen pluginler gözükür. (Pluginin Name ve Message alanları gözükür.)
  • Çalıştırmak istediğimizde pluginin Execute metodu çalışır.

Plugin Projelerin Pathleri

  1. HelloPlugin\bin\Debug\net8.0\HelloPlugin.dll
  2. GithubPlugin\bin\Debug\net8.0\GithubPlugin.dll

Ekran Görüntüleri

imagej image image image

console-plugin-example's People

Contributors

ufukgulec avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.