Skip to content

Commit 7417513

Browse files
author
Per Gårdebrink
committed
Make it possible to use factory for classes without public empty constructor
1 parent cc5a4c9 commit 7417513

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

src/CommandLine/Core/InstanceBuilder.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public static ParserResult<T> Build<T>(
3939

4040
Func<T> makeDefault = () =>
4141
typeof(T).IsMutable()
42-
? factory.MapValueOrDefault(f => f(), Activator.CreateInstance<T>())
42+
? factory.MapValueOrDefault(f => f(), () => Activator.CreateInstance<T>())
4343
: ReflectionHelper.CreateDefaultImmutableInstance<T>(
4444
(from p in specProps select p.Specification.ConversionType).ToArray());
4545

@@ -128,7 +128,7 @@ public static ParserResult<T> Build<T>(
128128

129129
private static T BuildMutable<T>(Maybe<Func<T>> factory, IEnumerable<SpecificationProperty> specPropsWithValue, List<Error> setPropertyErrors )
130130
{
131-
var mutable = factory.MapValueOrDefault(f => f(), Activator.CreateInstance<T>());
131+
var mutable = factory.MapValueOrDefault(f => f(), () => Activator.CreateInstance<T>());
132132

133133
setPropertyErrors.AddRange(
134134
mutable.SetProperties(

src/CommandLine/Infrastructure/CSharpx/Maybe.cs

+8
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,14 @@ public static T2 MapValueOrDefault<T1, T2>(this Maybe<T1> maybe, Func<T1, T2> fu
371371
return maybe.MatchJust(out value1) ? func(value1) : noneValue;
372372
}
373373

374+
/// <summary>
375+
/// If contains a values executes a mapping function over it, otherwise returns the value from <paramref name="noneValueFactory"/>.
376+
/// </summary>
377+
public static T2 MapValueOrDefault<T1, T2>(this Maybe<T1> maybe, Func<T1, T2> func, Func<T2> noneValueFactory) {
378+
T1 value1;
379+
return maybe.MatchJust(out value1) ? func(value1) : noneValueFactory();
380+
}
381+
374382
/// <summary>
375383
/// Returns an empty list when given <see cref="CSharpx.Nothing{T}"/> or a singleton list when given a <see cref="CSharpx.Just{T}"/>.
376384
/// </summary>

0 commit comments

Comments
 (0)