locked
reflection using a property name as string RRS feed

  • Question

  • I have an existing system that using xelements and an configuration file to flatten that xml to a flat file of a fixed length layout.  I want to replace the XElement with a generic object.   In order to do this properly I have to be able to take a string name "AddressLine1" and get the value of the property AddressLine1 (for example the way WPF does binding for a screen to a generic object).

    The second issue is that I need to get a List<myClass> within that object and iterate over it.   I will know the name,  eg "detail" but I will not know the type, in example <myClass>.   Again can I grab this and use it as a collection like

    foreach( object worker in parameter_object.somemagic("detail") )

    {

    }

    Ultimately I want the code to work on linq to Entities as well,  so either enumerable or List.


    Ta Ken
    Monday, October 18, 2010 12:00 AM

Answers

  • Take a look at "BindingFlags' and PropertyInfo

    const BindingFlags PublicPropertyBindings = 
      BindingFlags.Public
      | BindingFlags.GetProperty
      | BindingFlags.Instance;
    
    
    PropertyInfo theProperty = someObject.GetType().GetProperty(somePropertyName, PublicPropertyBindings);
    object theValue = theProp.GetValue(someObject, null);
    

    in your foreach example,

    const BindingFlags PublicPropertyBindings = 
      BindingFlags.Public
      | BindingFlags.GetProperty
      | BindingFlags.Instance;
    
    
    PropertyInfo theProperty = parameterObject.GetType().GetProperty("detail", PublicPropertyBindings);
    IEnumerable workers = theProp.GetValue(parameterObject, null) as IEnumerable;
    foreach(IEnumerable worker in workers)
    {
      foreach(object item in worker)
      {
        doSomethingWithWorkerItem(item);
      }
    }
    

    I am not sure this will do everything you are expecting.

    You might want to look at some dependency injection frameworks, such as Unity, Castle Windsor, or NInject.

    You also might want to look at the "Template Method" and "Strategy" patterns.

    You can create an interface:

    public interface IProcessor
    {
      void Process();
    }
     
    
    

    In your entity project add a new file to extend the generated partial entity classes:

    namespace MyEntities
    {
      public partial SomeEntity: IProcessor
      {
        public void Process()
        {
            // Do something with the entity
        }
      }      
    
      public partial SomeOtherEntity: IProcessor
      {
        public void Process()
        {
            // Do something with the entity
        }
      }
    }   
    

     


    gimme some slamming techno!!!!
    Monday, October 18, 2010 12:52 AM

All replies

  • I'm not sure I understnd exactly what you need, but this may help:

    http://msmvps.com/blogs/deborahk/archive/2010/08/28/iterate-through-the-properties-of-a-class.aspx

    It iterates through the properties of a class using Reflection.

    Hope this helps. 


    www.insteptech.com ; msmvps.com/blogs/deborahk
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    Monday, October 18, 2010 12:23 AM
  • Take a look at "BindingFlags' and PropertyInfo

    const BindingFlags PublicPropertyBindings = 
      BindingFlags.Public
      | BindingFlags.GetProperty
      | BindingFlags.Instance;
    
    
    PropertyInfo theProperty = someObject.GetType().GetProperty(somePropertyName, PublicPropertyBindings);
    object theValue = theProp.GetValue(someObject, null);
    

    in your foreach example,

    const BindingFlags PublicPropertyBindings = 
      BindingFlags.Public
      | BindingFlags.GetProperty
      | BindingFlags.Instance;
    
    
    PropertyInfo theProperty = parameterObject.GetType().GetProperty("detail", PublicPropertyBindings);
    IEnumerable workers = theProp.GetValue(parameterObject, null) as IEnumerable;
    foreach(IEnumerable worker in workers)
    {
      foreach(object item in worker)
      {
        doSomethingWithWorkerItem(item);
      }
    }
    

    I am not sure this will do everything you are expecting.

    You might want to look at some dependency injection frameworks, such as Unity, Castle Windsor, or NInject.

    You also might want to look at the "Template Method" and "Strategy" patterns.

    You can create an interface:

    public interface IProcessor
    {
      void Process();
    }
     
    
    

    In your entity project add a new file to extend the generated partial entity classes:

    namespace MyEntities
    {
      public partial SomeEntity: IProcessor
      {
        public void Process()
        {
            // Do something with the entity
        }
      }      
    
      public partial SomeOtherEntity: IProcessor
      {
        public void Process()
        {
            // Do something with the entity
        }
      }
    }   
    

     


    gimme some slamming techno!!!!
    Monday, October 18, 2010 12:52 AM
  • I found  FileHelpers on sourceforge that will help do some of the things I need.
    Ta Ken
    Tuesday, November 2, 2010 4:29 AM