locked
Get a property, save it's underlying object RRS feed

  • Question

  • User-978178259 posted

    I am trying to figure out a very generic way to save some data from an app I'm working on.  I'd like to be able to set a property of an object, only using it's name (as a string), and then save the object that it belongs to.  Below is some code to show what I'm trying to achieve.  

    In the Save() function of Foo I have some comments that say/show what I'd like to get working.
    Basically I want to have a generic function to save my objects, which takes the value to save and the property name to assign it to.  It then finds the property, sets the value, and then saves which ever object that property belongs to.

    public class Person
    {
        public Address HomeAddress { get; set; }
        public Person() { HomeAddress = new Address(); }
    }
    
    public class Address
    {
        public string Street { get; set; }
        public string Street2 { get; set; }
        public Address() { }
        public void Save() { /* Do Save stuff */ }
    }
    
    public class Foo
    {
        public Person person { get; set; }
        public Foo()
        {
            person = new Person();
            Save("123 Test Street", "Street");
        }
    
        public void Save(string value, string propertyName)
        {
            PropertyInfo[] propInfos = person.GetType().GetProperties();
            PropertyInfo info = GetProperty(propInfos, propertyName);
    
            // info is returned with the information for the Street property in HomeAddress.
            // Is there a way to set HomeAddress.Street = value, and then call HomeAddress.Save() using Reflection?
        }
    
        private PropertyInfo GetProperty(PropertyInfo[] pis, string propertyName)
        {
            foreach (PropertyInfo pi in pis)
            {
                if (pi.Name == propertyName)
                {
                    return pi;
                }
                else
                {
                    if (pi.PropertyType.FullName.StartsWith("MyLibraryName"))
                    {
                        PropertyInfo p2 = GetProperty(pi.PropertyType.GetProperties(), propertyName);
                        if (p2 != null)
                            return p2;
                    }
                }
            }
            return null;
        }
    }

    Thursday, April 21, 2011 4:02 PM

Answers

  • User-525215917 posted

    Why don't you use some O/R-mapper like NHibernate or Entity Framework? They have all this functionality built in and optimized way better than you can do it during first versions of your system. I strongly suggest you to use existing solutions and save a lot of time.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 25, 2011 3:38 AM

All replies

  • User-525215917 posted

    Why don't you use some O/R-mapper like NHibernate or Entity Framework? They have all this functionality built in and optimized way better than you can do it during first versions of your system. I strongly suggest you to use existing solutions and save a lot of time.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 25, 2011 3:38 AM
  • User-851968219 posted

    Look at this link to look at how to invoke method using reflection

    http://msdn.microsoft.com/en-us/library/a89hcwhh.aspx

     

    as for setting the properties you can do something like this
    if (info.CanWrite)
    {
        this.info.SetValue(myObject, myValue, null);
    }

    Monday, April 25, 2011 1:24 PM
  • User-952121411 posted

    I think this code is what you want; read through the article and take a look at the "UpdatePropertyUsingReflection" method:

    Using Reflection to Map Properties on Objects by their String Name:

    http://allen-conway-dotnet.blogspot.com/search/label/reflection

    Hope this helps! Smile

    Monday, April 25, 2011 2:35 PM