diff --git a/osu.Framework.Tests/Dependencies/Reflection/DependencyContainerTest.cs b/osu.Framework.Tests/Dependencies/Reflection/DependencyContainerTest.cs index 4bee79d3ed..924f6532d5 100644 --- a/osu.Framework.Tests/Dependencies/Reflection/DependencyContainerTest.cs +++ b/osu.Framework.Tests/Dependencies/Reflection/DependencyContainerTest.cs @@ -301,10 +301,7 @@ public void TestDependenciesOverrideParent(string name, Type parent) dependencies = new DependencyContainer(dependencies); dependencies.CacheAs(2, info); - Assert.Multiple(() => - { - Assert.AreEqual(2, dependencies.Get(info)); - }); + Assert.Multiple(() => { Assert.AreEqual(2, dependencies.Get(info)); }); } [Test] @@ -333,8 +330,19 @@ public void TestResolveNullStruct() Assert.That(new DependencyContainer().Get(), Is.Null); } + [Test] + public void TestModifyBoxedStruct() + { + var dependencies = new DependencyContainer(); + dependencies.CacheAs(new BaseStructObject { TestValue = 1 }); + dependencies.Get().TestValue = 2; + + Assert.That(dependencies.Get().TestValue, Is.EqualTo(2)); + } + private interface IBaseInterface { + int TestValue { get; set; } } private class BaseObject @@ -344,6 +352,7 @@ private class BaseObject private struct BaseStructObject : IBaseInterface { + public int TestValue { get; set; } } private class DerivedObject : BaseObject diff --git a/osu.Framework.Tests/Dependencies/SourceGeneration/DependencyContainerTest.cs b/osu.Framework.Tests/Dependencies/SourceGeneration/DependencyContainerTest.cs index 9aaa9355e5..30ed15d9db 100644 --- a/osu.Framework.Tests/Dependencies/SourceGeneration/DependencyContainerTest.cs +++ b/osu.Framework.Tests/Dependencies/SourceGeneration/DependencyContainerTest.cs @@ -304,10 +304,7 @@ public void TestDependenciesOverrideParent(string name, Type parent) dependencies = new DependencyContainer(dependencies); dependencies.CacheAs(2, info); - Assert.Multiple(() => - { - Assert.AreEqual(2, dependencies.Get(info)); - }); + Assert.Multiple(() => { Assert.AreEqual(2, dependencies.Get(info)); }); } [Test] @@ -336,8 +333,19 @@ public void TestResolveNullStruct() Assert.That(new DependencyContainer().Get(), Is.Null); } + [Test] + public void TestModifyBoxedStruct() + { + var dependencies = new DependencyContainer(); + dependencies.CacheAs(new BaseStructObject { TestValue = 1 }); + dependencies.Get().TestValue = 2; + + Assert.That(dependencies.Get().TestValue, Is.EqualTo(2)); + } + private interface IBaseInterface { + int TestValue { get; set; } } private class BaseObject @@ -347,6 +355,7 @@ private class BaseObject private struct BaseStructObject : IBaseInterface { + public int TestValue { get; set; } } private class DerivedObject : BaseObject diff --git a/osu.Framework/Allocation/DependencyContainer.cs b/osu.Framework/Allocation/DependencyContainer.cs index 10da5b4495..30a77fb412 100644 --- a/osu.Framework/Allocation/DependencyContainer.cs +++ b/osu.Framework/Allocation/DependencyContainer.cs @@ -44,11 +44,29 @@ public void Cache(object instance, CacheInfo info) CacheAs(instance.GetType(), info, instance); } + /// + /// Caches an instance of a type as its most derived type. This instance will be returned each time you . + /// + /// The instance to cache. + public void Cache(T instance) + where T : notnull + => Cache(instance, default); + + /// + /// Caches an instance of a type as its most derived type. This instance will be returned each time you . + /// + /// The instance to cache. + /// Extra information to identify in the cache. + public void Cache(T instance, CacheInfo info) + where T : notnull + => CacheAs(instance.GetType(), info, instance); + /// /// Caches an instance of a type as a type of . This instance will be returned each time you . /// /// The instance to cache. Must be or derive from . public void CacheAs(T instance) + where T : notnull => CacheAs(instance, default); /// @@ -57,6 +75,7 @@ public void CacheAs(T instance) /// The instance to cache. Must be or derive from . /// Extra information to identify in the cache. public void CacheAs(T instance, CacheInfo info) + where T : notnull => CacheAs(typeof(T), info, instance); /// @@ -65,6 +84,7 @@ public void CacheAs(T instance, CacheInfo info) /// The type to cache as. /// The instance to cache. Must be or derive from . public void CacheAs(Type type, T instance) + where T : notnull => CacheAs(type, instance, default); /// @@ -74,6 +94,7 @@ public void CacheAs(Type type, T instance) /// The instance to cache. Must be or derive from . /// Extra information to identify in the cache. public void CacheAs(Type type, T instance, CacheInfo info) + where T : notnull => CacheAs(type, info, instance); ///