0

Suppose there is a static object with type A.

class A
{
    public string b;
    public int c;
    public bool d;
    public A e;
    .
    .
    .
}

A a = new A(){
    b = "string",
    c = 12,
    d = true
    e = new A(){
            b = "another string",
            c = 23
        }
};

I want to deep clone this object into a dynamic object with all of its properties.

2
  • Let me get this straight. Are you saying that a is a static property or member variable of some other class? If so, write a clone method in class A that returns a deep clone of type A and assign it to anything you want to assign it to. All that the modifier static on a property or member variable means is that the property / member variable belongs to the class, not to an instance of the class. Other than that, the object referenced by that property/member variable is just an object. Commented Apr 18, 2014 at 15:08
  • Do you have any non-string reference properties? If so how do you want them to be handled? Create a copy of the object they point to or point to the same instance that the static property does? Commented Apr 18, 2014 at 15:18

4 Answers 4

1

I would enumerate the properties of the object (a.GetType().GetProperties()), destinguish between built-in types, structs and classes and use ExpandoObject to build the dynamic object.

Sign up to request clarification or add additional context in comments.

3 Comments

Is there any library to do that?
My problem is that I have a complex model which has multiple views. I want to build dynamic views from the model and then serialize them as JSON using Json.net. Json.net has the mechanism for custom serialization but it is not straight forward to build multiple types of serialization from a single model in different scenarios.
In different scenarios, I have to serialize some portion of the complex object and not the entire object. Serializing the entire object is so heavy and some times takes a few minutes.
1

The easiest way is to serialize the class into a json and deserialize it into a dynamic object.

Use Json.net:

        A a = new A()
        {
            b = "string",
            c = 12,
            d = true,
            e = new A()
            {
                b = "another string",
                c = 23
            }
        };

        var json = JsonConvert.SerializeObject(a); // create a json 
        dynamic newObj = JsonConvert.DeserializeObject(json);// create a dynamic object

3 Comments

My problem is that I have a complex model which has multiple views. I want to build dynamic views from the model and then serialize them as JSON using Json.net. Json.net has the mechanism for custom serialization but it is not straight forward to build multiple types of serialization from a single model in different scenarios.
A serialization process is recursive and can handle complex types. You will have problems with type such as dictionaries and stuff which you will need to explicitly implement an interface that will tell the serializer how to handle it. I dont see any problem with "complex" objects that cannot be solved(if you dont have circular references).
There is no problem with complex types. The Problem is the multiple view of the same model. In different scenarios, I have to serialize some portion of the complex object and not the entire object. Serializing the entire object is so heavy and some times takes a few minutes.
0

Build a copy constructor:

class A   //<-- this class is static?????
{
  public string b;
  public int c;
  public bool d;
  public A e;

  public A() { }  // add default constructor

  public A(A a){
     b = a.b;
     c = a.c;
     d = a.d;
     if ( a.e != null ) {
        e = new A(a.e);
     }
  }
}

Comments

0

In C#4.0, we have the concept of dynamic, but how to create a dynamic object from a static static object?

Below code will generate exception at run time. The dynamic object is from C# class, but it could be object from other languages through Dynamic Language Runtime (DLR). The point is not how to invoke static method, but how to invoke static method of dynamic object which could not be created in C# code.

class Foo { public static int Sum(int x, int y) { return x + y; } }

class Program {

static void Main(string[] args)
{
    dynamic d = new Foo();
    Console.WriteLine(d.Sum(1, 3));

}

}

dynamic type is invented as bridge between C# and other programming language. There is some other language (e.g. Java) allows to invoke static method through object instead of type.

for more details please visit enter link description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.