Posted By: Zeus | Oct 19th @ 4:03 AM
page 1 of 1
Comments: 15 | Views: 439
Zeus
Zeus
Why is the caption missing??

Hi guys and gals.

 

I am having a problem with reflection, and accessing sub-classes. Let's take a look at this structure:

 

public class Master { 
    public int ID { get; set; } 
    public string Name { get; set; } 
    public Child FirstChild { get; set; } 
} 
 
public class Child { 
    public int ID { get; set; } 
    public string Name { get; set; } 
} 

 

If I want to get the value of the Name property on the master class I can do this:

 

string name = item.GetType().GetProperty("Name").GetValue(item, null).ToString();

 

But If I want to access the value of the Name property of the FirstChild, I am stuck. I have read StackOverflow from cover to cover trying to find out how to do this ... do you have any insights ?

 

And I know reflection is performance heavy, but it's used in very few cases in our system, but getting this to work would simplify things a great deal.

TommyCarlier
TommyCarlier
I want my scalps!

object child =
  item
    .GetType()
    .GetProperty("FirstChild")
    .GetValue(item, null);

if (child != null)
{
  string childName =
    child
      .GetType()
      .GetProperty("Name")
      .GetValue(child, null) as string;
}

TommyCarlier
TommyCarlier
I want my scalps!

Thanks for the bug fix, I corrected my code. That's what happens when you improvize in the C9 editor. Smiley

Maybe it's easier to make a method to do this (so you can reuse it):

public static object GetPropertyValue(object item, string propertyName)
{
  return item != null
    ? item
        .GetType()
        .GetProperty(propertyName)
        .GetValue(item, null)
    : null;
}

Which you can the use like this:

var childName = 
  GetPropertyValue(
    GetPropertyValue(item, "FirstChild"),
    "Name") as string;

Yggdrasil
Yggdrasil
Pour me a cab, 'cause I can't drink no more.

I'm with Tommy here - if you're going to do any serious work with reflection, building a set of easy-access methods is essential, otherwise you'll get bogged down in tedious syntax and methods with 5-6 parameters, half of which are usually null or identical from call to call. Make your life easier, make a ReflectionHelper class with some helper methods. 

Wait I'm confused. Why can't you just say

 

var childName = parent.FirstChild.Name;

 

?

Because he's in a scenario where he doesn't know the types at compile time.

 

public void DoSomething(object parent)

{

}

 

There are lots of scenarios where reflection is very handy, if not essential. It comes at a heavy price in runtime performance, but that's a trade off that is often worth the price.

stevo_
stevo_
Human after all

I wouldn't go nuts Tongue Out

Ah that makes sense then.

 

I think VB's late binding would take care of that for me. Runtime error if the property doesn't exist.

Sven Groot
Sven Groot
My name has 9 letters. Coincidence? I think not...

Peanuts aren't nuts. Tongue Out

Sven Groot
Sven Groot
My name has 9 letters. Coincidence? I think not...

VB's late binding won't help you if you don't know the name of the property apriori.

But if Zeus doesn't know the name of the property he's after, the function Tommy gave won't help him either.

 

In VB I can compile

 


Public Sub(parent as Object)

   Dim name as String = parent.FirstChild.Name

End Sub

 

If parent doesn't have a FirstChild property then an exception is thrown at runtime.

Sven Groot
Sven Groot
My name has 9 letters. Coincidence? I think not...

For your VB snippet, you need to know the property's name at compile time.

 

For Tommy's function (and reflection in general), it's sufficient to know the property's name at run time, you don't need to know it at compile time.

 

That's the difference.

*light bulb moment*

 

Gotcha. I fail to see when I would ever need this, but I understand this thread now.

page 1 of 1
Comments: 15 | Views: 439
Microsoft Communities