Odd null reference from Type.GetProperty(string) method

Dec 9 2010 12:08 PM


One of my objects takes in a generic list collection of objects and an array of property names, and exposes a property 'SelectedPropertiesMeta' that returns a collection of PropertyInfo objects that represent selected properties a user can change at runtime. This is used to dynamically create a table at runtime, allowing the user to both choose certain items to display and arrange the columns in any order they like. Here's the class for the runtime object in question:

 
 

 
using
System.Collections.Generic;
using
System.Reflection;
 
namespace
Utilities
{
public class OrderedFields<T> where T : new()
{
public OrderedFields() { }
public OrderedFields(List<T> objects, string[] selectedProperties)
{
this.Objects = objects;
this.SelectedProperties = selectedProperties;
}
public List<T> Objects { get; set; }
public string[] SelectedProperties { get; set; }
public PropertyInfo[] SelectedPropertiesMeta
{
get
{
PropertyInfo[] result = new PropertyInfo[SelectedProperties.Length];
for (int index = 0; index < SelectedProperties.Length; index++)
{
result[index] =
typeof(T).GetProperty(SelectedProperties[index]);
}
return result;
}
}
}
}


 
The issue is occuring on the line that calls the GetProperty method. As an example, I have four properties selected for a Project object, and expect to get the PropertyInfo objects for each property. I'm getting the ID property PropertyInfo returned without issue, but the other three are returning null. I've read that this should only occur if the string name of the property isn't a valid name of a public property in the class. I've checked just to make certain, and this is not a problem. Here's the Project class:

 
 

public
class Project
{
public Project() { }
public int ID { get; set; }
public string Name { get; set; }
public string CourseNumber { get; set; }
public string Status { get; set; }
public string Stakeholder { get; set; }
public string Classification { get; set; }
public string Program { get; set; }
public string Description { get; set; }
public DateTime? DateCreated { get; set; }
public DateTime? StartDateEstimate { get; set; }
public DateTime? EndDateEstimate { get; set; }
public DateTime? StartDateActual { get; set; }
public DateTime? EndDateActual { get; set; }
public int Weight { get; set; }
public DateTime? LastUpdated { get; set; }
}
 

 
It's not complicated. In the OrderedFields class, I'm trying to get the PropertyInfo's for ID, Name, CourseNumber and Status, all of which are valid public property names. The SelectedProperties collection is correctly storing the selected properties, but only the ID is getting returned correctly. When I step through the code, the other three are returning null. No exception is being thrown. Besides an invalid property name, what else could cause this issue?

Answers (4)