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:
TypeScript
usingSystem.Collections.Generic;
usingSystem.Reflection;
namespaceUtilities
{
publicclassOrderedFields where T : new()
{
publicOrderedFields() { }
publicOrderedFields(List objects, string[] selectedProperties)
{
this.Objects = objects;
this.SelectedProperties = selectedProperties;
}
publicListObjects { get; set; }
publicstring[] SelectedProperties { get; set; }
publicPropertyInfo[] SelectedPropertiesMeta
{
get
{
PropertyInfo[] result = newPropertyInfo[SelectedProperties.Length];
for (int index = 0; 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:
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?
4 Answers
Know the answer? Post it. Somebody with the same question will find it here.
William Snell15 reputationPosted Dec 9, 2010, 8:16 AM EST
0
I've done some testing, and it seems that no matter what the first property is that is listed, that one has a PropertyInfo object successfully retrieved for it, but all the remaining items return null. I'm still trying to figure out why.
William Snell15 reputationPosted Dec 9, 2010, 10:21 AM EST
0
Thanks, and yes, I have values assigned to those properties. It wouldn't matter if I did not, however. Since I'm only grabbing information about the metadata at this point, and not the values contained, I don't need assigned values. For instance, I have this implementation in an MVC application:
C#
public ActionResult Index()
{
// Code to get list of projects goes here.
projects.Clear();
projects.Add(new Project
{
ID = 0,
Name = "some project",
CourseNumber = "course number",
DateCreated = DateTime.Now
});
projects.Add(new Project
{
ID = 1,
Name = "some project 2",
CourseNumber = "course number 2"
});
OrderedFields orderedFields =
new OrderedFields(projects, columns);
return View(orderedFields);
}
Now, this works just fine, and the table rendered simply emits an empty string for the DateCreated in the second Project object. In fact, within an isolated project containing only minimal MVC code, the two classes for the ordered fields and the project class, this all works without a problem. Somehow, in my main project, it is not working. Since I copied the exact code from the main project into the example project, I'm completely stupified as to what is going wrong. I've zipped up the project so you can take a look at it. I worked this out after someone very talented on the ASP.NET forums provided another implementation. It's very handy if you need to reorder columns of a rendered table at runtime.
William Snell15 reputationPosted Dec 9, 2010, 12:17 PM EST
0
I missed something small, but that should have been obvious. The only thing that could have been wrong was the property names, but they appeared to be correct. When I looked closer, I noticed leading spaces in front of all but the first property names. This, of course, resulted in a non-match to any property names in the class I passed in. I wrote an extension method that serialized List collections into comma-separated values for storage, and didn't realize I'd added an extra space where it shouldn't have been. Something that small, and it had me in fits for 3 hours. I need to grab some food before starting back up.
William Snell15 reputationPosted Dec 9, 2010, 8:16 AM EST
Shahan AyyubRank #5,163 · 309 reputationPosted Dec 9, 2010, 9:05 AM EST
I have tested your project, Can you please make sure that you have assigned values to the ID, CourseNumber etc ??? like:
List
Project proj = new Project();
proj.ID = 100;
proj.CourseNumber = "100";
proj.Name = "abc";
p.Add(proj);
string[] properties = new string[] { "ID","Name", "CourseNumber" };
OrderedFields
William Snell15 reputationPosted Dec 9, 2010, 10:21 AM EST
public ActionResult Index() { // Code to get list of projects goes here. projects.Clear(); projects.Add(new Project { ID = 0, Name = "some project", CourseNumber = "course number", DateCreated = DateTime.Now }); projects.Add(new Project { ID = 1, Name = "some project 2", CourseNumber = "course number 2" }); OrderedFields orderedFields = new OrderedFields(projects, columns); return View(orderedFields); }Now, this works just fine, and the table rendered simply emits an empty string for the DateCreated in the second Project object. In fact, within an isolated project containing only minimal MVC code, the two classes for the ordered fields and the project class, this all works without a problem. Somehow, in my main project, it is not working. Since I copied the exact code from the main project into the example project, I'm completely stupified as to what is going wrong. I've zipped up the project so you can take a look at it. I worked this out after someone very talented on the ASP.NET forums provided another implementation. It's very handy if you need to reorder columns of a rendered table at runtime.
William Snell15 reputationPosted Dec 9, 2010, 12:17 PM EST