Link to home
Start Free TrialLog in
Avatar of emw1
emw1

asked on

c# reflection

I am venturing into C# reflection.  I have figured out to get the properties, and set them but not how to add them back to the class.  Any help appreciated
public class SessionProperties
      {
            public string ToSectionId { get; set; }
            public string FromSectionId { get; set; }
            public string TemplateId { get; set; }
            public string PanelToEnable { get; set; }
            public string TemplateTypeId { get; set; }
      }
 
 public class SessionClass
      {
            public SessionProperties Section { get; set; }
            public SessionProperties SubSection { get; set; }
            public SessionProperties SubSubSection { get; set; }
            public SessionProperties ChapterMenu { get; set; }
            public SessionProperties Scroller { get; set; }
            public SessionProperties RelatedContent { get; set; }
      }
 
public static void SetSessionClassProperty(string sessionPropertyObjectType, string propertyName, string propertyValue)
            {
                  if (System.Web.HttpContext.Current.Session["SessionClass"] != null)
                  {
 
                        //we have 2 classes to loop thru 
                        // the SessionClass
                        //and the SessionProperties
 
                        //first the SessionClass
                        SessionClass thisClass = (SessionClass)System.Web.HttpContext.Current.Session["SessionClass"];
                        //loop thru properties
                        Type thisClassType = thisClass.GetType();
 
 
                        PropertyInfo[] thisClassProperties = thisClassType.GetProperties();
 
                        // Now let's create a new instance of this Type.
                                    object SessionClassObj = Activator.CreateInstance(thisClassType);
 
                        foreach (PropertyInfo thisClassProperty in thisClassProperties)
                        {
                              //get designated SessionProperties
                              if (thisClassProperty.Name == sessionPropertyObjectType)
                              {
                                    //now loop thru the SessionProperties properties
                                    Type sessPropType = typeof(SessionProperties);
                                    PropertyInfo[] sessPropProperties = sessPropType.GetProperties();
 
                                    // Now let's create a new instance of this Type.
                                    object obj = Activator.CreateInstance(sessPropType);
 
                                    foreach (PropertyInfo sessPropProperty in sessPropProperties)
                                    {
                                          if (sessPropProperty.Name == propertyName)
                                          {
                                                sessPropProperty.SetValue(obj, propertyValue, null);
                                                
                                          }
                                       //   thisClassProperty.ReflectedType.Name
                                      
                                    }
                                    //now add the property back to the class
                                   //can't figure out how to do this
                                   
 
                              }
                        }
                        //thisClass is not updated when code executes
                      System.Web.HttpContext.Current.Session["SessionClass"] = thisClass;
                  }
                  
 
            }

Open in new window

Avatar of wht1986
wht1986
Flag of United States of America image

Below is my code how I did this.  1st I added some code to initialize SessionProperties classes of the SessionClass class. Otherwise i had nulls.  But basically since you know the property of the first loop is a SessionProperties class, go ahead and get the value and then you can do a setvalue on it.  Hopefully the code below explains it.
public void SetSessionClassProperty(string sessionPropertyObjectType, string propertyName, string propertyValue)
{
 
    SessionClass thisClass = (SessionClass)System.Web.HttpContext.Current.Session["SessionClass"];
    Type thisClassType = thisClass.GetType();
 
    PropertyInfo[] thisClassProperties = thisClassType.GetProperties();
 
    foreach (PropertyInfo thisClassProperty in thisClassProperties)
    {
        //get designated SessionProperties
        if (thisClassProperty.Name == sessionPropertyObjectType)
        {
 
            SessionProperties targetSessionProperty = (SessionProperties)thisClassProperty.GetValue(thisClass, null);
 
            //now loop thru the SessionProperties properties
            Type sessPropType = targetSessionProperty.GetType();
            PropertyInfo[] sessPropProperties = sessPropType.GetProperties();
 
            foreach (PropertyInfo sessPropProperty in sessPropProperties)
            {
                if (sessPropProperty.Name == propertyName)
                    sessPropProperty.SetValue(targetSessionProperty, propertyValue, null);
            }
        }
    }
 
    System.Web.HttpContext.Current.Session["SessionClass"] = thisClass;
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of wht1986
wht1986
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of emw1
emw1

ASKER

Thank You - It worked Great!