Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
Expand All @@ -52,7 +52,7 @@ protected override void AddInterfaceType(Type interfaceType)
return;
}

base.AddInterfaceType(interfaceType);
base.AddInterfaceType(interfaceType, throwException);
}
}
}
51 changes: 49 additions & 2 deletions VContainer/Assets/VContainer/Runtime/RegistrationBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@ public RegistrationBuilder As<TInterface1, TInterface2, TInterface3>()
public RegistrationBuilder As<TInterface1, TInterface2, TInterface3, TInterface4>()
=> As(typeof(TInterface1), typeof(TInterface2), typeof(TInterface3), typeof(TInterface4));

public RegistrationBuilder TryAs<TInterface>()
=> TryAs(typeof(TInterface));

public RegistrationBuilder TryAs<TInterface1, TInterface2>()
=> TryAs(typeof(TInterface1), typeof(TInterface2));

public RegistrationBuilder TryAs<TInterface1, TInterface2, TInterface3>()
=> TryAs(typeof(TInterface1), typeof(TInterface2), typeof(TInterface3));

public RegistrationBuilder TryAs<TInterface1, TInterface2, TInterface3, TInterface4>()
=> TryAs(typeof(TInterface1), typeof(TInterface2), typeof(TInterface3), typeof(TInterface4));

public RegistrationBuilder AsSelf()
{
AddInterfaceType(ImplementationType);
Expand Down Expand Up @@ -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<IInjectParameter>();
Expand Down Expand Up @@ -127,11 +169,16 @@ public RegistrationBuilder WithParameter<TParam>(Func<TParam> 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<Type>();
if (!InterfaceTypes.Contains(interfaceType))
Expand Down
Loading