@@ -14,6 +14,7 @@ namespace Bunit
1414 /// <typeparam name="TComponent">The type of component under test to add the parameters</typeparam>
1515 public sealed class ComponentParameterCollectionBuilder < TComponent > where TComponent : IComponent
1616 {
17+ private static readonly Type TComponentType = typeof ( TComponent ) ;
1718 private const string ChildContent = nameof ( ChildContent ) ;
1819
1920 /// <summary>
@@ -303,6 +304,38 @@ public ComponentParameterCollectionBuilder<TComponent> AddUnmatched(string name,
303304 return AddParameter ( name , value ) ;
304305 }
305306
307+ /// <summary>
308+ /// Try to add a <paramref name="value"/> for a parameter with the <paramref name="name"/>, if
309+ /// <typeparamref name="TComponent"/> has a property with that name, AND that property has a <see cref="ParameterAttribute"/>
310+ /// or a <see cref="CascadingParameterAttribute"/>.
311+ /// </summary>
312+ /// <remarks>
313+ /// This is an untyped version of the <see cref="Add{TValue}(Expression{Func{TComponent, TValue}}, TValue)"/> method. Always
314+ /// prefer the strongly typed <c>Add</c> methods whenever possible.
315+ /// </remarks>
316+ /// <typeparam name="TValue">Value type.</typeparam>
317+ /// <param name="name">Name of the property for the parameter.</param>
318+ /// <param name="value">Value to assign to the parameter</param>
319+ /// <returns>True if parameter with the name exists and value was set, false otherwise.</returns>
320+ public bool TryAdd < TValue > ( string name , [ AllowNull ] TValue value )
321+ {
322+ if ( TComponentType . GetProperty ( name , BindingFlags . Public | BindingFlags . Instance ) is PropertyInfo ccProp )
323+ {
324+ if ( ccProp . GetCustomAttribute < ParameterAttribute > ( inherit : false ) is not null )
325+ {
326+ AddParameter ( name , value ) ;
327+ return true ;
328+ }
329+
330+ if ( ccProp . GetCustomAttribute < CascadingParameterAttribute > ( inherit : false ) is CascadingParameterAttribute cpa )
331+ {
332+ AddCascadingValueParameter ( cpa . Name , value ) ;
333+ return true ;
334+ }
335+ }
336+ return false ;
337+ }
338+
306339 /// <summary>
307340 /// Builds the <see cref="ComponentParameterCollection"/>.
308341 /// </summary>
@@ -325,8 +358,8 @@ private static (string paramName, string? cascadingValueName, bool isCascading)
325358 }
326359
327360 private static bool HasChildContentParameter ( )
328- => typeof ( TComponent ) . GetProperty ( ChildContent , BindingFlags . Public | BindingFlags . Instance ) is PropertyInfo ccProp
329- && ccProp . GetCustomAttribute < ParameterAttribute > ( inherit : false ) != null ;
361+ => TComponentType . GetProperty ( ChildContent , BindingFlags . Public | BindingFlags . Instance ) is PropertyInfo ccProp
362+ && ccProp . GetCustomAttribute < ParameterAttribute > ( inherit : false ) is not null ;
330363
331364 private ComponentParameterCollectionBuilder < TComponent > AddParameter < TValue > ( string name , [ AllowNull ] TValue value )
332365 {
0 commit comments