?
Solved

c# reflection

Posted on 2009-05-06
3
Medium Priority
?
297 Views
Last Modified: 2012-05-06
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

0
Comment
Question by:emw1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 12

Expert Comment

by:wht1986
ID: 24319230
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

0
 
LVL 12

Accepted Solution

by:
wht1986 earned 1000 total points
ID: 24319306
Having said that, there is no need to loop unless its dynamic and there is a chance the string might not be found.  If you know you will always send valid arguements to the method, i would cut to the chase and use the below
public void SetSessionClassProperty(string sessionPropertyObjectType, string propertyName, string propertyValue)
{
    SessionClass thisClass = (SessionClass)System.Web.HttpContext.Current.Session["SessionClass"];
 
    Type SessionClassType = (new SessionClass()).GetType();
    Type SessionPropertiesType = (new SessionProperties()).GetType();
 
    SessionProperties targetSessionProperty = (SessionProperties)SessionClassType.GetProperty(sessionPropertyObjectType).GetValue(thisClass, null);
    SessionPropertiesType.GetProperty(propertyName).SetValue(targetSessionProperty, propertyValue, null);
 
    System.Web.HttpContext.Current.Session["SessionClass"] = thisClass;
}

Open in new window

0
 

Author Closing Comment

by:emw1
ID: 31578651
Thank You - It worked Great!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses
Course of the Month14 days, 7 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question