Thanks to visit codestin.com
Credit goes to github.com

Skip to content

RegisterSingletonAttribute support export static object #247

@MikiraSora

Description

@MikiraSora

In MEF there is a export example like this:

[Export]
 public static MenuBarDefinition MainMenuBar = new MenuBarDefinition();

Injectio does not support this operation, but we can implement it. Theoretically, by using the following code:

namespace Gemini.Modules.MainMenu
{
  public static class MenuDefinitions
  {
    [RegisterSingleton<MenuDefinition>]
    public static MenuDefinition MainMenuBar = new MenuBarDefinition();
  }
}

we can then generate

global::Microsoft.Extensions.DependencyInjection.Extensions.ServiceCollectionDescriptorExtensions.TryAdd(
                serviceCollection,
             global::Microsoft.Extensions.DependencyInjection.ServiceDescriptor.Scoped<global::Gemini.Framework.Menus.MenuBarDefinition>(_=>Gemini.Modules.MainMenu.MenuDefinitions.MainMenuBar)
            );

Therefore, Injectio can extend the RegisterSingletonAttribute. This attribute can decorate fields and auto-properties. they must be public and static, and cannot be null.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions