ScholarQuill logoScholarQuillUniversity Notes
  • Notes
  • Past Papers
  • Blogs
  • Todo
Login
ScholarQuill logoScholarQuillUniversity Notes
Login
NotesPast PapersBlogsTodo
More
SubjectsDiscussionCGPA CalculatorGPA CalculatorStudent PortalCourse Outline
About
About usPrivacy PolicyReportContact
Notes
Past Papers
Blogs
Todo
Analytics
    Current Subject
    🧩
    Advanced Programming
    CSI-415
    Progress0 / 55 topics
    Topics
    1. Visual Programming Basics2. Introduction to Events3. Fundamentals of Event-Driven Programming4. Message Handling5. User Interfaces6. Graphics Device Interface7. Painting and Drawing8. Windows Management9. Input Devices10. Resources11. String and Menu Resource12. Dialogs and Windows Controls13. Common Controls14. Dynamic Link Libraries (DLLs)15. Threads and Synchronization16. Network Programming17. Building Class Libraries at the Command Line18. Class Libraries19. Using References20. Assemblies21. Private Assembly Deployment22. Shared Assembly Deployment23. Configuration Overview24. Configuration Files25. Programmatic Access to Configuration26. Using SDK Tools for Signing and Deployment27. Metadata28. Reflection29. Late Binding30. Directories and Files31. Serialization32. Attributes33. Memory Management and Garbage Collection34. Threading and Synchronization35. Asynchronous Delegates36. Application Domains37. Marshal by Value38. Marshal by Reference39. Authentication and Authorization40. Configuring Security41. Code Access Security42. Code Groups43. Evidence44. Permissions45. Role-Based Security46. Principals and Identities47. Using Data Readers48. Using Data Sets49. Interacting with XML Data50. Tracing Event Logs51. Using the Boolean Switch and Trace Switch Classes52. Print Debugging Information with the Debug Class53. Instrumenting Release Builds with the Trace Class54. Using Listeners55. Implementing Custom Listeners
    CSI-415›Marshal by Value
    Advanced ProgrammingTopic 37 of 55

    Marshal by Value

    7 minread
    1,265words
    Intermediatelevel

    Marshal by Value in C#

    In C#, marshalling by value refers to the process of copying an object's data (value type) into another memory location, such as when passing data across application domain boundaries, from a managed environment to unmanaged code, or across network boundaries. When an object is marshaled by value, the entire object or its data is copied from one context to another, and any changes made to the object in the target domain do not affect the original object in the source domain.

    Key Concepts

    1. Marshalling: The process of transforming data into a suitable format for transmission across different contexts, such as application domains, processes, or between managed and unmanaged code.

    2. Marshal by Value: This involves copying the data of an object to another memory location rather than passing a reference to the object. The target receives a duplicate of the object, and any changes made to the copy do not affect the original object.

    3. Value Types: These are types where variables directly hold the data, not references to the data. Examples of value types are primitive types like int, double, and struct types. When a value type is passed by value, the value itself is copied.

    4. Reference Types: These are types where variables hold references (or pointers) to the data. Examples of reference types include classes, arrays, and delegates. By default, reference types are passed by reference, which means that changes made to the object affect the original instance.

    1. Marshal by Value in Application Domains

    When an object is marshaled by value across application domains (using MarshalByValue), the object's data is copied to the new application domain, and the source domain retains its version of the object. The object in the destination domain will be a separate instance from the original.

    In an Application Domain scenario, the object is serialized (its state is saved) and transferred to the new domain, where it is deserialized (reconstructed). This is called passing by value because the data is copied, and there is no shared reference between the two application domains.

    Example of Marshal by Value (Application Domain Example)

    using System;
    
    public class MyClass
    {
        public int Value;
    }
    
    public class Program
    {
        public static void Main()
        {
            // Create a new application domain
            AppDomain newAppDomain = AppDomain.CreateDomain("MyNewAppDomain");
    
            // Create an instance of MyClass in the main AppDomain
            MyClass myObject = new MyClass { Value = 100 };
    
            // Marshal by value: create a copy of the object in the new AppDomain
            MyClass myObjectInNewDomain = (MyClass)newAppDomain.CreateInstanceAndUnwrap(
                typeof(MyClass).Assembly.FullName, "MyClass");
    
            // Marshal the state by copying the value to the new object
            myObjectInNewDomain.Value = myObject.Value;
    
            // Print values from both domains
            Console.WriteLine("Value in main AppDomain: " + myObject.Value);
            Console.WriteLine("Value in new AppDomain: " + myObjectInNewDomain.Value);
    
            // Unload the application domain
            AppDomain.Unload(newAppDomain);
        }
    }
    

    In the above example:

    • myObject is created in the main application domain, with a value of 100.
    • myObjectInNewDomain is created in a new application domain and receives a copy of the data from myObject.
    • After setting the value of myObjectInNewDomain, you can see that both objects are independent, and changes in one do not affect the other.

    2. MarshalByValue vs. MarshalByReference

    When marshalling data, there are two common approaches:

    1. Marshal by Value: The data is copied, and the target has a separate instance of the object.
    2. Marshal by Reference: A reference to the original object is passed, so changes made in the target context are reflected in the original object.

    To compare:

    • Marshal by Value: The target gets a copy of the object, and modifications to the copy do not affect the original object.
    • Marshal by Reference: The target gets a reference to the original object, and changes to the object are reflected in both contexts.

    You can specify MarshalByValue by using classes that are designed for cross-domain marshalling, such as those derived from MarshalByValueObject.

    3. MarshalByValueObject Class

    In .NET, the MarshalByValueObject class is a base class designed for objects that should be marshaled by value. Any object derived from this class can be marshaled by value across application domains.

    using System;
    
    public class MyClass : MarshalByValueObject
    {
        public int Value { get; set; }
    
        public MyClass(int value)
        {
            Value = value;
        }
    }
    
    public class Program
    {
        public static void Main()
        {
            MyClass myObject = new MyClass(42);
    
            // Marshal the object by value (copy its state)
            MyClass myObjectCopy = (MyClass)myObject.Clone();
    
            Console.WriteLine($"Original object value: {myObject.Value}");
            Console.WriteLine($"Cloned object value: {myObjectCopy.Value}");
    
            // Modify the cloned object
            myObjectCopy.Value = 100;
    
            Console.WriteLine($"After modification:");
            Console.WriteLine($"Original object value: {myObject.Value}");
            Console.WriteLine($"Cloned object value: {myObjectCopy.Value}");
        }
    }
    

    In this example, MyClass is derived from MarshalByValueObject. When you clone the object (Clone() method), the original object and its copy are independent, showing that they hold separate data. Modifying the cloned object doesn't affect the original.

    4. Marshalling in Remoting

    In .NET Remoting, marshalling by value is used when passing objects between remoting servers. The object is serialized and sent over the network, creating a copy in the destination process.

    Example:

    using System;
    using System.Runtime.Remoting;
    
    public class MyClass : MarshalByValueObject
    {
        public int Value { get; set; }
    
        public MyClass(int value)
        {
            Value = value;
        }
    }
    
    public class Program
    {
        public static void Main()
        {
            MyClass myObject = new MyClass(10);
            
            // Marshal by value across remoting boundary
            RemotingServices.Marshal(myObject, "MyObject");
    
            // Display object value
            Console.WriteLine($"Value: {myObject.Value}");
        }
    }
    

    5. Serialization for Marshal by Value

    When you marshal an object by value across application domains or processes, the object’s data is serialized (converted into a transferable format) and deserialized (converted back into an object) on the receiving end. Serialization is the process that underlies marshalling by value, ensuring that objects can be copied and transferred.

    • Binary Serialization: For marshaling by value in memory or across networks, you often use BinaryFormatter to serialize and deserialize objects.

    Example:

    using System;
    using System.IO;
    using System.Runtime.Serialization;
    using System.Runtime.Serialization.Formatters.Binary;
    
    [Serializable]
    public class MyClass
    {
        public int Value { get; set; }
    
        public MyClass(int value)
        {
            Value = value;
        }
    }
    
    public class Program
    {
        public static void Main()
        {
            MyClass original = new MyClass(42);
    
            // Serialize to memory stream
            IFormatter formatter = new BinaryFormatter();
            using (MemoryStream stream = new MemoryStream())
            {
                formatter.Serialize(stream, original);
    
                // Reset the stream to the beginning
                stream.Seek(0, SeekOrigin.Begin);
    
                // Deserialize into a new object
                MyClass copy = (MyClass)formatter.Deserialize(stream);
    
                // Modify the copy
                copy.Value = 100;
    
                Console.WriteLine($"Original Value: {original.Value}");
                Console.WriteLine($"Copied Value: {copy.Value}");
            }
        }
    }
    

    6. Summary

    • Marshal by Value: When objects are passed by value, a copy of the data is made, and the two objects (original and copy) are independent. Any changes to the copy do not affect the original object.
    • MarshalByValueObject: A class in .NET that is used to define objects that should be marshaled by value.
    • Serialization: For marshalling by value, objects are serialized into a format that can be transmitted or copied across application boundaries and later deserialized.
    • Applications: Marshal by value is often used in scenarios like remoting, passing objects between application domains, or copying data across different processes.

    By understanding marshalling by value, you can manage how objects are passed across boundaries while ensuring that data remains isolated and safe in different contexts.

    Previous topic 36
    Application Domains
    Next topic 38
    Marshal by Reference

    Past Papers

    Open this section to load past papers

    Click on Show Past Papers to see past papers.
    On This Page
      Reading Stats
      Est. reading time7 min
      Word count1,265
      Code examples0
      DifficultyIntermediate