I have a c# class that gets object property names.
public class MyClass
{
public int Id { get; set; }
public int Name { get; set; }
}
public class Parser<T>
{
public IEnumerable<string> Parse(T item)
{
const BindingFlags flag = BindingFlags.Instance | BindingFlags.Public;
return item.GetType().GetProperties(flag).Select(p => p.Name);
}
}
I want to test this methot.
[Test]
public void TestMethod()
{
var parser = new Parser<MyClass>();
var names = parser.Parse(new MyClass()).ToArray();
Assert.IsTrue(names[1] == "Name");
}
But this throws an exception System.NullReferenceException. But I put breakpoint and see names
array have two items.
Assert.IsTrue(names[1] == "Name");
row throws exception. But is has 2 items.
Test 'M:ParsProejct.TestsClass.TestMethod.' failed: Object reference not set to an instance of an object.