Marshalling is as process of making an object avail across network or application domain. Marshaling is the act of taking data from the environment you are in and exporting it to another environment.
UnMarshalling creates an object from the marshaled data.
There are 2 ways of Marshalling:
Marshal by Value: In this, the object is serialized into the channel, and a copy of the object is created on the other side of the network. The object to marshal is stored into a stream, and the stream is used to build a copy of the object on the other side with the unmarshalling sequence.Value types are marshalled to unmanaged code on the stack.
Marshal by Reference: Here it creates a proxy on the client that is used to communicate with the remote object. The marshaling sequence of a remote object creates an ObjRef instance that itself can be serialized across the network. Objects that are derived from “MarshalByRefObject” are always marshaled by reference. To marshal a remote object the static method RemotingServices.Marshal () is used. Reference types are passed by address. This means a pointer is passed on the stack, and the pointer contains the address of the marshaled data on the heap. When parameters are passed by reference, a pointer to the parameters on the managed heap is passed to the unmanaged code. Since the unmanaged code receives a pointer, it is possible for the method to modify the data held on the managed heap.Blittable : A type is considered blittable if it has a common representation in managed and unmanaged code memory. Non-blittable types require custom marshaling to convert between the unmanaged and managed representations.
[InAttribute] and [OutAttribute]
These are called as directional attributes because the control marshalling direction.
[InAttribute] tells the CLR to marshal data from the caller to callee at the begining of the call, while [OutAttribute] tells the CLR to marshal data from the caller to callee upon return. Caller and callee can be either unmanaged or managed code.(Ex. In a P/Invoke call managed code calls unmanaged code, but in reverse P/Invoke unmanaged code calls managed code through function pointer.)
Why can't I serialize .NET Framework classes like exceptions and fonts?
The XmlSerializer is primarily designed with two goals in mind: XML data binding to XSD compliant data structures and operation without any special code access privileges. These two goals work against the XmlSerializer as a general-purpose object persistence solution for some kinds of objects.
General purpose serialization may require accessing private fields, by-passing the framework's standard object construction process, and so on, which in turn requires special privileges. The SoapFormatter from the System.Runtime.Serialization.Formatters.Soap namespace provides an alternative that is not subject to these restrictions, but requires full trust to operate. It also produces an XML format, a generation of which is customizable using the attributes in the System.Runtime.Remoting.Metadata namespace.
How do I serialize collections of objects?
The XmlSerializer throws an exception when the collection contains types that were not declared to the constructor of the XmlSerializer. You can:
->Declare the types to the serializer by passing in a Type[] with the types to expect within the collection.
OR
->Implement a strongly-typed collection derived from System.Collections.CollectionBase with an indexer matching the Add() method.
Why can't I serialize hashtables?
The XmlSerializer cannot process classes implementing the IDictionary interface. This was partly due to schedule constraints and partly due to the fact that a hashtable does not have a counterpart in the XSD type system. The only solution is to implement a custom hashtable that does not implement the IDictionary interface.
Why do exceptions thrown by the XmlSerializer not contain any details about the error?
They do contain all the information, but it's stored in the InnerException property of the exception thrown, which is usually an InvalidOperationException. In general, one should always call ToString() on caught exceptions to get the full details of the exception.
Generic delegates:
As we do event-based programming or implement publish-subscribe pattern, we use the delegate feature from .NET heavily. The code below shows how we can use delegate:
public delegate void MydDelegate(int oldValue, int newValue);
class TestClass
{
public MydDelegate OnDataChanged = null;
private int _mydata = 0;
public int MyData
{
get { return _mydata; }
set { UpdateData(value); }
}
private void UpdateData(int updatedData)
{
if (OnDataChanged != null)
{
OnDataChanged(_data, updatedData);
}
_data = updatedData;
}
}
We can then create an instance of TestClass ,and hook up the event handler to its public delegate as shown below.
class Program
{
static void Main(string[] args)
{
TestClass obj = new TestClass();
obj.OnDataChanged += OnDataChanged;
obj.Data = 5;
}
static void OnDataChanged(int oldValue, int newValue)
{
Console.WriteLine("Data changes from " + oldValue + " to " + newValue);
}
}
C# 3.0 or .NET 3.5 has new feature that provides us a set of generic delegates with the name "Action" or "Func", the difference between them is Action does not have return type (void), while Func has return type (refer to MSDN documentation). So from example above we can remove following code:
public delegate void DataChangedDelegate(int oldValue, int newValue);
and replace following code:
public DataChangedDelegate OnDataChanged = null;
with:
public Action
If the delegate returns bool data type for example, then we can use Func function such as:
public Func
wonderful article. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. This article resolved my all queries. keep it up.
ReplyDeleteDot Net Training in Chennai | Dot Net Training in anna nagar | Dot Net Training in omr | Dot Net Training in porur | Dot Net Training in tambaram | Dot Net Training in velachery