You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
recyclarr/src/tests/Recyclarr.TestLibrary/AutoFixture/CustomizeWithAttribute.cs

32 lines
938 B

using System.Reflection;
using AutoFixture;
namespace Recyclarr.TestLibrary.AutoFixture;
// Based on the answer here: https://stackoverflow.com/a/16735551/157971
[AttributeUsage(AttributeTargets.Parameter, AllowMultiple = true)]
public sealed class CustomizeWithAttribute : CustomizeAttribute
{
public Type CustomizationType { get; }
public CustomizeWithAttribute(Type customizationType)
{
if (customizationType == null)
{
throw new ArgumentNullException(nameof(customizationType));
}
if (!typeof(ICustomization).IsAssignableFrom(customizationType))
{
throw new ArgumentException("Type needs to implement ICustomization");
}
CustomizationType = customizationType;
}
public override ICustomization? GetCustomization(ParameterInfo parameter)
{
return (ICustomization?) Activator.CreateInstance(CustomizationType);
}
}