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

  • Home
  • SEARCH
  • 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 8588085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:43:39+00:00 2026-06-11T22:43:39+00:00

I have a problem cloning dynamic object with the code like this: public void

  • 0

I have a problem cloning dynamic object with the code like this:

    public void Execute(IPrepareData entity)
    {
        try
        {                
            dynamic data = entity.Primary as dynamic;
            data.PreviousInfo = deepClone(data.Info);
        }
        catch (Exception ex)
        {
            data.Errors.Add(ex.Message);
        }
    }

    private static T deepClone<T>(T obj)
    {
        if (typeof(T).IsClass || typeof(T).IsArray)
        {
            if (ReferenceEquals(obj, null))
            {
                return default(T);
            }
        }
        using (var memoryStream = new MemoryStream())
        {
            BinaryFormatter fieldFormatter = new BinaryFormatter();
            fieldFormatter.Serialize(memoryStream, obj);
            memoryStream.Position = 0;
            return (T)fieldFormatter.Deserialize(memoryStream);
        }
    }

    dynamic data;

I don’t know the structure of entity in advance (only that it will contain Info, and I don’t know the structure of info) and that it won’t be marked serializable. I need to copy this info to previous info section of entity.

Result of execution of this code is ‘Object reference not set to an instance of an object’ on fieldFormatter.Serialize line.

How can I check if it is an instance of an object?

There might be (most probably will be) circular references, so I am not trying reflection as I am not sure how to deal with that. Also speed is not an issue.

  • 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-06-11T22:43:40+00:00Added an answer on June 11, 2026 at 10:43 pm

    If you don’t know that the data will be marked serializable, then you can’t rely on using BinaryFormatter.

    If the object is likely to have circular references, a lot of other serializers are out of the question.

    If we assume it is the general case of dynamic (and not just ExpandoObject), then there is no way of getting information about the members, since they can be invented as they are queried.

    Basically, this scenario *has no good answer. There is no magic way to just deep clone “a thing”.

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

Sidebar

Related Questions

I have problem with my query on C, I’m using the oci8 driver. This
I have problem while loading data into html select when users press or click
I have problem SIMILAR to preventing form data reposting, but not quite the same
I have another problem regarding Git. This time I thoroughly searched Google and Stack
I have some problems cloning an object hierarchie. It's a toolkit for modelling applications,
I have some basic classes with cloning methods: public class SimpleClass { public int
I have a class that goes like this: function Element(){ this.changes = {}; }
I have this infoflyout.ascx <%@ Control Language=C# Inherits=System.Web.Mvc.ViewUserControl<dynamic> %> <script type=text/javascript> $(document).ready(function () {
Problem for me is to implement clone for an object. This object A has
I have following problem; I have inerface with public method clone(): public interface Something

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.