0

I have a class that contains another poco class with simple get set properties:

public class PersonalInformation { public string FirstName { get; set; } public string FirstSomethingElse { get; set; } } 

I would like to find out if the current instance's PersonalInformation.FirstName has a value. I can't figure out how to obtain it via reflection:

foreach (PropertyInfo property in this.PersonalInformation.GetType().GetProperties()) { if (property.Name.Contains("First")) { if (property.GetValue(XXX, null) != null) do something... } } 

The instance I have is "this", which does not work, neither does this.PersonalInformation. What am I doing wrong?

Thank you for your response,

Aldo

Addendum: I'm using ASP.NET MVC3. In my razor view I can do the following very easily:

foreach (var property in Model.PersonalInformation.GetType().GetProperties()) { <div class="editor-line"> @if (property.Name != null) { <label>@(property.Name)</label> @Html.Editor(property.Name) } </div> } 

there is a property.Value member that returns the current value of the field. This field comes from a poco class, as you see above. What would be the equivalent code in the code-behind?

7
  • 3
    what do you mean by "doesn't work" Commented Dec 12, 2012 at 20:43
  • 2
    Why are you using reflection? Commented Dec 12, 2012 at 20:47
  • What have you tried? Do you get an exception? Should you be using reflection? Commented Dec 12, 2012 at 20:48
  • >> What do you mean by "doesn't work?" - means I'm getting an object exception Commented Dec 12, 2012 at 20:59
  • >> Why are you using reflection? : this is an ultrasimplified example. Let's pretend I have hundreds of properties in my form. Commented Dec 12, 2012 at 21:00

2 Answers 2

4

this.PersonalInformation certainly should work. After all, that's the target you're talking about.

Sample code:

using System; using System.Reflection; public class PersonalInformation { public string FirstName { get; set; } public string FirstSomethingElse { get; set; } } public class Foo { public PersonalInformation PersonalInformation { get; set; } public void ShowProperties() { foreach (var property in this.PersonalInformation .GetType() .GetProperties()) { var value = property.GetValue(this.PersonalInformation, null); Console.WriteLine("{0}: {1}", property.Name, value); } } } class Test { static void Main() { Foo foo = new Foo { PersonalInformation = new PersonalInformation { FirstName = "Fred", FirstSomethingElse = "XYZ" } }; foo.ShowProperties(); } } 

Although if you just "want to find out if the current instance's PersonalInformation.FirstName has a value" then I don't see why you're using reflection...

Sign up to request clarification or add additional context in comments.

Comments

-3

GetProperties returns a PropertyInfo[], not a single PropertyInfo.

1 Comment

... which is why he's iterating over them.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.