Click here to Skip to main content
15,904,348 members
Home / Discussions / C#
   

C#

 
GeneralRe: Path of running application. Pin
Derek Bartram30-Apr-08 5:29
Derek Bartram30-Apr-08 5:29 
GeneralRe: Path of running application. Pin
Member 404498830-Apr-08 6:00
Member 404498830-Apr-08 6:00 
GeneralRe: Path of running application. Pin
Derek Bartram30-Apr-08 6:27
Derek Bartram30-Apr-08 6:27 
GeneralRe: Path of running application. Pin
Member 404498830-Apr-08 10:25
Member 404498830-Apr-08 10:25 
GeneralRe: Path of running application. Pin
Derek Bartram1-May-08 1:59
Derek Bartram1-May-08 1:59 
GeneralRe: Path of running application. Pin
Member 40449881-May-08 9:15
Member 40449881-May-08 9:15 
GeneralRe: Path of running application. Pin
Derek Bartram1-May-08 9:48
Derek Bartram1-May-08 9:48 
GeneralGet/Set object properties using 'System.Reflection' Pin
Lea Hayes25-Apr-08 13:10
Lea Hayes25-Apr-08 13:10 
Hi all!

I have written a method which allows me to access nested properties within objects. The function which I have written returns a PropertyDescriptor, the property component, and property value when given an object and property string.

So for example, if I had an object called "person" it would be possible to access a particular property like:

object component = null, value = null;

PropertyDescriptor desc = person.FindPropertyDescriptor("Occupation.Address.Road", ref component, ref value);

// In effect this is the equivalent of:
object value = person.Occupation.Address.Road;


Here is the function which I have written:

/// <summary>
/// Finds property descriptor of object from a property name string.
/// </summary>
/// <param name="obj">Object for which to search.</param>
/// <param name="propertyName">Property, i.e. "Size.Width".</param>
/// <param name="nestedObject">Outputs nested component.</param>
/// <param name="nestedValue">Outputs component value.</param>
/// <returns>Property descriptor.</returns>
public static PropertyDescriptor FindPropertyDescriptor(this object obj, string propertyName, ref object nestedComponent, ref object nestedValue)
{
    // Find required property.
    object activeObject = null, nextObject = obj;

    PropertyDescriptor propDesc = null;
    string propNamePart = "";
    int lastIndex = 0, nextIndex = 0;

    while (nextIndex != -1)
    {
        // Initialize active object.
        activeObject = nextObject;

        // Fetch next property name substring.
        nextIndex = propertyName.IndexOf('.', lastIndex);
        if (nextIndex != -1)
        {
            propNamePart = propertyName.Substring(lastIndex, nextIndex - lastIndex);
            lastIndex = nextIndex + 1;
        }
        else
            propNamePart = propertyName.Substring(lastIndex, propertyName.Length - lastIndex);

        // Fetch nested property.
        PropertyDescriptorCollection activeProperties = TypeDescriptor.GetProperties(activeObject);
        foreach (PropertyDescriptor pDesc in activeProperties)
        {
            if (pDesc.DisplayName == propNamePart)
            {
                propDesc = pDesc;
                break;
            }
        }
        nextObject = propDesc.GetValue(activeObject);
    }
    nestedComponent = activeObject;
    nestedValue = nextObject;
    return propDesc;
}


Is there an easier .NET way of doing the same thing?

Any suggestions would be fantastic!

Lea Hayes
GeneralRe: Get/Set object properties using 'System.Reflection' Pin
Derek Bartram26-Apr-08 7:47
Derek Bartram26-Apr-08 7:47 
GeneralRe: Get/Set object properties using 'System.Reflection' Pin
Lea Hayes27-Apr-08 4:10
Lea Hayes27-Apr-08 4:10 
GeneralRe: Get/Set object properties using 'System.Reflection' Pin
Derek Bartram27-Apr-08 6:52
Derek Bartram27-Apr-08 6:52 
GeneralLoading an C# DLL at runtime with C# Pin
FreewareFire25-Apr-08 12:47
FreewareFire25-Apr-08 12:47 
GeneralRe: Loading an C# DLL at runtime with C# Pin
Thomas Stockwell25-Apr-08 13:55
professionalThomas Stockwell25-Apr-08 13:55 
GeneralRe: Loading an C# DLL at runtime with C# Pin
Derek Bartram26-Apr-08 7:48
Derek Bartram26-Apr-08 7:48 
GeneralCodeMethodInvokeExpression problem, Write dt.Load(dr) code currectly. Pin
hdv21225-Apr-08 11:47
hdv21225-Apr-08 11:47 
QuestionVisual Studio 2008: Design- Data Grid View Pin
Hamid intact25-Apr-08 11:30
Hamid intact25-Apr-08 11:30 
GeneralUsing System.IO.Packaging Pin
Wjousts25-Apr-08 9:29
Wjousts25-Apr-08 9:29 
GeneralRe: Using System.IO.Packaging Pin
KaptinKrunch25-Apr-08 10:37
KaptinKrunch25-Apr-08 10:37 
GeneralRe: Using System.IO.Packaging Pin
Wjousts25-Apr-08 10:57
Wjousts25-Apr-08 10:57 
QuestionHow I use an Applicaton Role? Pin
aecordoba25-Apr-08 9:08
aecordoba25-Apr-08 9:08 
GeneralRe: How I use an Applicaton Role? Pin
KaptinKrunch25-Apr-08 10:27
KaptinKrunch25-Apr-08 10:27 
GeneralRe: How I use an Applicaton Role? Pin
aecordoba25-Apr-08 10:59
aecordoba25-Apr-08 10:59 
Generalhelp plz Pin
hhani25-Apr-08 8:08
hhani25-Apr-08 8:08 
GeneralRe: help plz Pin
carbon_golem25-Apr-08 8:26
carbon_golem25-Apr-08 8:26 
GeneralRe: help plz Pin
Colin Angus Mackay25-Apr-08 8:56
Colin Angus Mackay25-Apr-08 8:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.