From cf089a0268cbbda9949efea6e9d2005b5864369a Mon Sep 17 00:00:00 2001 From: Jonah Trias Date: Thu, 12 Jun 2025 11:54:01 +0100 Subject: [PATCH] Added TryAs in RegistrationBuilder - a version of As that doesn't throw --- .../OpenGenericRegistrationBuilder.cs | 4 +- .../VContainer/Runtime/RegistrationBuilder.cs | 51 ++++++++++++++++++- 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/VContainer/Assets/VContainer/Runtime/Internal/OpenGenericRegistrationBuilder.cs b/VContainer/Assets/VContainer/Runtime/Internal/OpenGenericRegistrationBuilder.cs index a4639cbc..44cc14f6 100644 --- a/VContainer/Assets/VContainer/Runtime/Internal/OpenGenericRegistrationBuilder.cs +++ b/VContainer/Assets/VContainer/Runtime/Internal/OpenGenericRegistrationBuilder.cs @@ -31,7 +31,7 @@ public override RegistrationBuilder AsImplementedInterfaces() return this; } - protected override void AddInterfaceType(Type interfaceType) + protected override void AddInterfaceType(Type interfaceType, bool throwException = true) { if (interfaceType.IsConstructedGenericType) throw new VContainerException(interfaceType, "Type is not open generic type."); @@ -52,7 +52,7 @@ protected override void AddInterfaceType(Type interfaceType) return; } - base.AddInterfaceType(interfaceType); + base.AddInterfaceType(interfaceType, throwException); } } } \ No newline at end of file diff --git a/VContainer/Assets/VContainer/Runtime/RegistrationBuilder.cs b/VContainer/Assets/VContainer/Runtime/RegistrationBuilder.cs index 4f59a0a1..5e25a267 100644 --- a/VContainer/Assets/VContainer/Runtime/RegistrationBuilder.cs +++ b/VContainer/Assets/VContainer/Runtime/RegistrationBuilder.cs @@ -41,6 +41,18 @@ public RegistrationBuilder As() public RegistrationBuilder As() => As(typeof(TInterface1), typeof(TInterface2), typeof(TInterface3), typeof(TInterface4)); + public RegistrationBuilder TryAs() + => TryAs(typeof(TInterface)); + + public RegistrationBuilder TryAs() + => TryAs(typeof(TInterface1), typeof(TInterface2)); + + public RegistrationBuilder TryAs() + => TryAs(typeof(TInterface1), typeof(TInterface2), typeof(TInterface3)); + + public RegistrationBuilder TryAs() + => TryAs(typeof(TInterface1), typeof(TInterface2), typeof(TInterface3), typeof(TInterface4)); + public RegistrationBuilder AsSelf() { AddInterfaceType(ImplementationType); @@ -84,6 +96,36 @@ public RegistrationBuilder As(params Type[] interfaceTypes) return this; } + public RegistrationBuilder TryAs(Type interfaceType) + { + AddInterfaceType(interfaceType, false); + return this; + } + + public RegistrationBuilder TryAs(Type interfaceType1, Type interfaceType2) + { + AddInterfaceType(interfaceType1, false); + AddInterfaceType(interfaceType2, false); + return this; + } + + public RegistrationBuilder TryAs(Type interfaceType1, Type interfaceType2, Type interfaceType3) + { + AddInterfaceType(interfaceType1, false); + AddInterfaceType(interfaceType2, false); + AddInterfaceType(interfaceType3, false); + return this; + } + + public RegistrationBuilder TryAs(params Type[] interfaceTypes) + { + foreach (var interfaceType in interfaceTypes) + { + AddInterfaceType(interfaceType, false); + } + return this; + } + public RegistrationBuilder WithParameter(string name, object value) { Parameters = Parameters ?? new List(); @@ -127,11 +169,16 @@ public RegistrationBuilder WithParameter(Func value) return WithParameter(typeof(TParam), _ => value()); } - protected virtual void AddInterfaceType(Type interfaceType) + protected virtual void AddInterfaceType(Type interfaceType, bool throwException = true) { if (!interfaceType.IsAssignableFrom(ImplementationType)) { - throw new VContainerException(interfaceType, $"{ImplementationType} is not assignable from {interfaceType}"); + if (throwException) + { + throw new VContainerException(interfaceType, $"{ImplementationType} is not assignable from {interfaceType}"); + } + + return; } InterfaceTypes = InterfaceTypes ?? new List(); if (!InterfaceTypes.Contains(interfaceType))