C# reflection problem - please help

Discussion in 'C#' started by dementic, Jan 12, 2011.

  1. #1
    Hi, please help me understand what's going on in here and whether it should work like that ? I have a generic list of objects from a CMS:

    For example List<MyCMS.Articles.Article> myArticles = articles.All;

    Later I output the contents of the list in a JSON format (for CMS UI - table list).

    Now a single record would include:

    
            MyCms.Content.Games games = new MyCms.Content.Games();
            List<MyCms.Content.Games.Game> allGames = games.All;
    
            MyCms.Content.Games games2 = new MyCms.Content.Games();
            List<MyCms.Content.Games.Game> allGamesOther = games2.All;
    
            Response.Write("Total games: " + allGames.Count + "<br />");
    
            //This is our fields stripper - with result assigned to a new list
            List<MyCms.Content.Games.Game> completelyUnrelatedOtherIsolated_but_notSureList = RemoveUnusedFields(allGamesOther);
    
            List<MyCms.Content.Games.Game> gamesFiltered = allGames.Where(g=>g.GamingProperties.Software=="89070ef9-e115-4907-9996-6421e6013993").ToList();
    
            Response.Write("Filtered games: " + gamesFiltered.Count + "<br /><br />");
    
        }
    
        private List<MyCms.Content.Games.Game> RemoveUnusedFields(List<MyCms.Content.Games.Game> games)
        {
            List<MyCms.Content.Games.Game> result = new List<MyCms.Content.Games.Game>();
    
            if (games != null && games.Count > 0)
            {
                //Retrieve a list of current object properties
                List<string> myContentProperties = MyCms.Utils.GetContentProperties(games[0]);
    
                //      Response.Write("<br /><span style=color:green>" + string.Join("<br />", myContentProperties.ToArray()) + "</span><br />");
    
                MyCms.PropertyReflector pF = new MyCms.PropertyReflector();
    
                foreach (MyCms.Content.Games.Game contentItem in games)
                {
                    MyCms.Content.Games.Game myNewGame = (MyCms.Content.Games.Game)contentItem.Clone();
                    myNewGame.Images = "wtf!";
    
                    pF.SetValue(myNewGame, "GamingProperties.Software", "");
    
                    result.Add(myNewGame);
                 
                }
            }
    
            return result;
        }
    
    Code (markup):
    etc...

    as you can see an Article object has an additional class property - with common properties (used on other object types in the CMS)

    I also use a filter class that does some filter operations with LINQ on the collection to return me only articles within a certain channel, for example...

    So the problem is that when I serialize the collection as JSON - there are only a few "columns" that I really need to display in my table list, while I have no need in other fields - especially, possibly long fields such as "description" (lazy loaded from file system), etc... - I serialize with DataContractJsonSerializer...

    I need a way to control what fields will be included in the JSON result... What I do is I use reflection to set property values to null if I don't need the property and decorate class properties with [DataMember(IsRequired = false, EmitDefaultValue = false)] attributes... - it should work well - but - as soon as I go over (even cloned!!) collection of final objects to strip off the fields = set value to "null" - property value becomes null - application wide - in all collections of such objects... eh ?
     
    dementic, Jan 12, 2011 IP
  2. dementic

    dementic Peon

    Messages:
    280
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #2
    problem solved,
    i needed to deep clone the classes.
     
    dementic, Jan 12, 2011 IP