diff --git a/Dalamud/Plugin/DalamudPluginInterface.cs b/Dalamud/Plugin/DalamudPluginInterface.cs index cbe1d9224..35b274665 100644 --- a/Dalamud/Plugin/DalamudPluginInterface.cs +++ b/Dalamud/Plugin/DalamudPluginInterface.cs @@ -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 + + /// + /// Create a new object of the provided type using its default constructor, then inject objects and properties. + /// + /// Objects to inject additionally. + /// The type to create. + /// The created and initialized type. + public T? Create(params object[] scopedObjects) where T : class + { + var svcContainer = Service.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; + } + + /// + /// Inject services into properties on the provided object instance. + /// + /// The instance to inject services into. + /// Objects to inject additionally. + /// Whether or not the injection succeeded. + public bool Inject(object instance, params object[] scopedObjects) + { + var svcContainer = Service.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 + /// /// Unregister your plugin and dispose all references. ///