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

ValueSourceAttribute

Indicates the source used to provide data for one parameter of a test method.
using NUnit.Framework.Interfaces; using NUnit.Framework.Internal; using NUnit.Framework.Internal.Extensions; using System; using System.Collections; using System.Reflection; using System.Runtime.CompilerServices; namespace NUnit.Framework { [System.Runtime.CompilerServices.NullableContext(2)] [System.Runtime.CompilerServices.Nullable(0)] [AttributeUsage(AttributeTargets.Parameter, AllowMultiple = true, Inherited = false)] public class ValueSourceAttribute : NUnitAttribute, IParameterDataSource { public string SourceName { get; } public Type SourceType { get; } public ValueSourceAttribute(string sourceName) { SourceName = sourceName; } public ValueSourceAttribute(Type sourceType, string sourceName) { SourceType = sourceType; SourceName = sourceName; } [System.Runtime.CompilerServices.NullableContext(1)] public IEnumerable GetData(IParameterInfo parameter) { return GetDataSource(parameter); } [System.Runtime.CompilerServices.NullableContext(1)] private IEnumerable GetDataSource(IParameterInfo parameter) { Type type = SourceType ?? parameter.Method.TypeInfo.Type; if (SourceName == null) { IEnumerable obj = Reflect.Construct(type) as IEnumerable; if (obj == null) { DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(56, 1); defaultInterpolatedStringHandler.AppendLiteral("The value source type '"); defaultInterpolatedStringHandler.AppendFormatted(type); defaultInterpolatedStringHandler.AppendLiteral("' does not implement IEnumerable."); throw new InvalidDataSourceException(defaultInterpolatedStringHandler.ToStringAndClear()); } return obj; } MemberInfo[] members = type.GetMemberIncludingFromBase(SourceName, BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); IEnumerable enumerable = ContextUtils.DoIsolated(() => GetDataSourceValue(members)); if (enumerable == null) throw CreateSourceNameException(); return enumerable; } [System.Runtime.CompilerServices.NullableContext(1)] [return: System.Runtime.CompilerServices.Nullable(2)] private static IEnumerable GetDataSourceValue(MemberInfo[] members) { if (members.Length != 1) return null; MemberInfo memberInfo = members[0]; FieldInfo fieldInfo = memberInfo as FieldInfo; if ((object)fieldInfo != null) { if (fieldInfo.IsStatic) return (IEnumerable)fieldInfo.GetValue(null); throw CreateSourceNameException(); } PropertyInfo propertyInfo = memberInfo as PropertyInfo; if ((object)propertyInfo != null) { if (propertyInfo.GetGetMethod(true)?.IsStatic ?? false) return (IEnumerable)propertyInfo.GetValue(null, null); throw CreateSourceNameException(); } MethodInfo methodInfo = memberInfo as MethodInfo; if ((object)methodInfo != null) { if (methodInfo.IsStatic) return AsyncEnumerableAdapter.CoalesceToEnumerable(methodInfo.InvokeMaybeAwait<object>()); throw CreateSourceNameException(); } return null; } [System.Runtime.CompilerServices.NullableContext(1)] private static InvalidDataSourceException CreateSourceNameException() { return new InvalidDataSourceException("The sourceName specified on a ValueSourceAttribute must refer to a non-null static field, property or method."); } } }