Skip to content

Commit afad336

Browse files
authored
Merge pull request #4 from jpdillingham/dev
Resolved a NullReferenceException when using Populate() with a target Type containing no properties marked with an OperandsAttribute.
2 parents 7f51e7c + 5f7d9f1 commit afad336

File tree

3 files changed

+38
-9
lines changed

3 files changed

+38
-9
lines changed

Utility.CommandLine.Arguments.Tests/Arguments.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,4 +539,28 @@ public void PopulateOperands()
539539

540540
#endregion Public Methods
541541
}
542+
543+
/// <summary>
544+
/// Unit tests for the <see cref="CommandLine.Arguments"/> class.
545+
/// </summary>
546+
/// <remarks>Used to facilitate testing of a class with no properties.</remarks>
547+
[Collection("Arguments")]
548+
public class TestClassWithNoProperties
549+
{
550+
#region Public Methods
551+
552+
/// <summary>
553+
/// Tests the <see cref="Utility.CommandLine.Arguments.Populate(Type, string)"/> method with an explicit string
554+
/// containing a single argument/value pair, and with a Type containing no properties.
555+
/// </summary>
556+
[Fact]
557+
public void Populate()
558+
{
559+
Exception ex = Record.Exception(() => CommandLine.Arguments.Populate(GetType(), "--hello world one two"));
560+
561+
Assert.Null(ex);
562+
}
563+
564+
#endregion Public Methods
565+
}
542566
}

Utility.CommandLine.Arguments/Arguments.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -312,13 +312,18 @@ public static void Populate(Type type, Arguments arguments)
312312

313313
PropertyInfo operandsProperty = GetOperandsProperty(type);
314314

315-
if (operandsProperty.PropertyType.IsAssignableFrom(typeof(List<string>)))
315+
// check to ensure the target class has a property marked with the Operands attribute; if not GetOperandsProperty()
316+
// will return null.
317+
if (operandsProperty != default(PropertyInfo))
316318
{
317-
operandsProperty.SetValue(null, arguments.OperandList);
318-
}
319-
else
320-
{
321-
operandsProperty.SetValue(null, arguments.OperandList.ToArray());
319+
if (operandsProperty.PropertyType.IsAssignableFrom(typeof(List<string>)))
320+
{
321+
operandsProperty.SetValue(null, arguments.OperandList);
322+
}
323+
else
324+
{
325+
operandsProperty.SetValue(null, arguments.OperandList.ToArray());
326+
}
322327
}
323328
}
324329

@@ -458,7 +463,7 @@ private static PropertyInfo GetOperandsProperty(Type type)
458463
.Any(a => a.AttributeType.Name == typeof(OperandsAttribute).Name))
459464
.FirstOrDefault();
460465

461-
if (property.PropertyType != typeof(string[]) && property.PropertyType != typeof(List<string>))
466+
if (property != default(PropertyInfo) && property.PropertyType != typeof(string[]) && property.PropertyType != typeof(List<string>))
462467
{
463468
throw new InvalidCastException("The target for the Operands attribute must be of string[] or List<string>.");
464469
}

Utility.CommandLine.Arguments/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@
1111
[assembly: AssemblyCulture("")]
1212
[assembly: ComVisible(false)]
1313
[assembly: Guid("b739dd22-4bd3-4d17-a351-33d129e9fe30")]
14-
[assembly: AssemblyVersion("1.1.1")]
15-
[assembly: AssemblyFileVersion("1.1.1")]
14+
[assembly: AssemblyVersion("1.1.2")]
15+
[assembly: AssemblyFileVersion("1.1.2")]

0 commit comments

Comments
 (0)