Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3679386
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T03:27:11+00:00 2026-05-19T03:27:11+00:00

please help me understand what’s going on in here and whether it should work

  • 0

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:

article.Title
article.Alias
article.Guid
article.Description
+
article.SeoProperties.TitleOverride
article.SeoProperties.H1Tag
article.StateProperties.IsActive
article.StateProperties.Channels

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 ?

Some demo code in here:

void Page_Load() {
        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]);

            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!"; //just to be sure we do set this stuff not only null

                pF.SetValue(myNewGame, "GamingProperties.Software", ""); //set one property to null for testing

                result.Add(myNewGame);

            }
        }

    return result;
}

Objects are set to their “Default values” (basically, null, in most cases) with this:

 private object GetDefaultValue(Type type)
        {
            if (type.IsValueType)
            {
                try
                {
                    return Activator.CreateInstance(type);
                }
                catch {
                    return null;
                }
            }

            return null;
        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T03:27:12+00:00Added an answer on May 19, 2026 at 3:27 am

    Quite probably your having trouble with differentiating between a shallow copy and a deep copy.

    If a field is a value type, a bit-by-bit copy of the field is performed. If a field is a reference type, the reference is copied but the referred object is not; therefore, the original object and its clone refer to the same object.

    With a deep copy when you clone an object and that object has a field of reference type, a new clone of that object is created and assigned to the field (vs just referencing the first object). Thus you have to completely different objects that share nothing.

    That means if you’re using clone and some of the properties are in fact subproperties (that is, properties of an instance inside the original object) you’re changing it application wide, because you’re acting upon a reference, not a new instance of the subobject.

    You have more information about it in

    http://msdn.microsoft.com/en-us/library/system.object.memberwiseclone.aspx

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please help me understand this. Here you can see that I have PYTHONPATH set
Please help me understand where I am going wrong so much so that I
Someone please help me understand why this binding does not work... I have a
please help me understand this. You have a function that calls a few methods:
Can you please help me understand the following code? It seems that here the
can someone please help me understand what's going on here lists:dropwhile(fun(X) -> X <
Please help me understand the following code snippet :- def any(l): whether any number
I'm a noob; please help me understand this authentication config / bindings stuff that
the following code is not behaving like I would expect. Please help me understand
Can anyone please help me to understand how should i access json data in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.