feat: DPI methods for injection

This commit is contained in:
goat 2021-08-24 00:55:35 +02:00
parent 48081bba7d
commit 6813212258
No known key found for this signature in database
GPG key ID: F18F057873895461

View file

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.Globalization;
using System.IO;
using System.Linq;
@ -16,6 +17,7 @@ using Dalamud.Game.Text.SeStringHandling.Payloads;
using Dalamud.Interface;
using Dalamud.Interface.Internal;
using Dalamud.Plugin.Internal;
using ServiceContainer = Dalamud.IoC.Internal.ServiceContainer;
namespace Dalamud.Plugin
{
@ -307,6 +309,44 @@ namespace Dalamud.Plugin
}
#endregion
#region Dependency Injection
/// <summary>
/// Create a new object of the provided type using its default constructor, then inject objects and properties.
/// </summary>
/// <param name="scopedObjects">Objects to inject additionally.</param>
/// <typeparam name="T">The type to create.</typeparam>
/// <returns>The created and initialized type.</returns>
public T? Create<T>(params object[] scopedObjects) where T : class
{
var svcContainer = Service<ServiceContainer>.Get();
var realScopedObjects = new object[scopedObjects.Length + 1];
realScopedObjects[0] = this;
Array.Copy(scopedObjects, 0, realScopedObjects, 1, scopedObjects.Length);
return svcContainer.Create(typeof(T), realScopedObjects) as T;
}
/// <summary>
/// Inject services into properties on the provided object instance.
/// </summary>
/// <param name="instance">The instance to inject services into.</param>
/// <param name="scopedObjects">Objects to inject additionally.</param>
/// <returns>Whether or not the injection succeeded.</returns>
public bool Inject(object instance, params object[] scopedObjects)
{
var svcContainer = Service<ServiceContainer>.Get();
var realScopedObjects = new object[scopedObjects.Length + 1];
realScopedObjects[0] = this;
Array.Copy(scopedObjects, 0, realScopedObjects, 1, scopedObjects.Length);
return svcContainer.InjectProperties(instance, realScopedObjects);
}
#endregion
/// <summary>
/// Unregister your plugin and dispose all references.
/// </summary>