Skip to content

Instantly share code, notes, and snippets.

@bluewalk
Created November 12, 2020 06:58
Show Gist options
  • Save bluewalk/b786de848134e34296da02d6337d30b2 to your computer and use it in GitHub Desktop.
Save bluewalk/b786de848134e34296da02d6337d30b2 to your computer and use it in GitHub Desktop.
GetServiceProvider

After building the HostProvider, assign the IServiceProvider to the class

GetServiceProvider._ = host.Services;

Then use anywhere in the application to DPI classes

GetServiceProvider.GetServiceOrCreateInstance<SomeClass>()
public static class GetServiceProvider
{
public static IServiceProvider _ { get; set; }
public static T GetServiceOrCreateInstance<T>()
{
return ActivatorUtilities.GetServiceOrCreateInstance<T>(_);
}
public static object GetServiceOrCreateInstance(Type type)
{
return ActivatorUtilities.GetServiceOrCreateInstance(_, type);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment