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:
|
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?
William SnellPosted Dec 9, 2010, 5:17 PM
William SnellPosted Dec 9, 2010, 3:21 PM
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:
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.
Shahan AyyubPosted Dec 9, 2010, 2:05 PM
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 SnellPosted Dec 9, 2010, 1:16 PM