<PackageReference Include="NUnit" Version="3.7.0" />

ValueSourceAttribute

ValueSourceAttribute indicates the source to be used to provide data for one parameter of a test method.
using NUnit.Framework.Interfaces; using NUnit.Framework.Internal; using System; using System.Collections; using System.Reflection; namespace NUnit.Framework { [AttributeUsage(AttributeTargets.Parameter, AllowMultiple = true, Inherited = false)] public class ValueSourceAttribute : DataAttribute, IParameterDataSource { public string SourceName { get; set; } public Type SourceType { get; set; } public ValueSourceAttribute(string sourceName) { SourceName = sourceName; } public ValueSourceAttribute(Type sourceType, string sourceName) { SourceType = sourceType; SourceName = sourceName; } public IEnumerable GetData(IParameterInfo parameter) { return GetDataSource(parameter); } private IEnumerable GetDataSource(IParameterInfo parameter) { Type type = SourceType ?? parameter.Method.TypeInfo.Type; if (SourceName == null) return Reflect.Construct(type) as IEnumerable; IEnumerable dataSourceValue = GetDataSourceValue(type.GetMember(SourceName, BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy)); if (dataSourceValue == null) ThrowInvalidDataSourceException(); return dataSourceValue; } private static IEnumerable GetDataSourceValue(MemberInfo[] members) { if (members.Length != 1) return null; MemberInfo memberInfo = members[0]; FieldInfo fieldInfo = memberInfo as FieldInfo; if (fieldInfo != (FieldInfo)null) { if (fieldInfo.IsStatic) return (IEnumerable)fieldInfo.GetValue(null); ThrowInvalidDataSourceException(); } PropertyInfo propertyInfo = memberInfo as PropertyInfo; if (propertyInfo != (PropertyInfo)null) { if (propertyInfo.GetGetMethod(true).IsStatic) return (IEnumerable)propertyInfo.GetValue(null, null); ThrowInvalidDataSourceException(); } MethodInfo methodInfo = memberInfo as MethodInfo; if (methodInfo != (MethodInfo)null) { if (methodInfo.IsStatic) return (IEnumerable)methodInfo.Invoke(null, null); ThrowInvalidDataSourceException(); } return null; } private static void ThrowInvalidDataSourceException() { throw new InvalidDataSourceException("The sourceName specified on a ValueSourceAttribute must refer to a non null static field, property or method."); } } }