Professional Documents
Culture Documents
A fixup is the process of finalizing an object reference after the referenced object
has been deserialized. Once the referenced object is deserialized, ObjectManager
completes the reference
Serializing Objects
• How to Create Classes That Can Be Serialized
You can serialize and deserialize custom classes by adding the Serializable attribute
to the class.This is important to do so that you, or other developers using your class,
can easily store or transfer instances of the class.
Some members of your class, such as temporary or calculated values,might not need
to be stored , So we can use the resources effectively. For these Add NonSerialized
attribute to these members and use the IDeserializationCallback interface, and then
implement IDeserializationCallback.OnDeserialization.
Serializing Objects
• How to Provide Version Compatibility
If you add a member to a class in version 3.1 of your application, it will not be able
to deserialize an object created by version 3.0 of your application.
Binary Formatter
Namespace: System.Runtime.Serialization.Formatters.Binary
Most Efficient way to serialize the objects that will be read by
only .Net based Application.
Soap Formatter
Namespace: System.Runtime.Serialization.Formatters.Soap
Most Reliable way to serialize the objects that can be read by
any non-.Net Framework Application
Serializing Objects
• Serialized data by using Soap Formatter
<SOAP-ENV:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<SOAP-ENV:Body>
<a1:ShoppingCartItem id="ref-1">
<productId>100</productId>
<price>10.25</price>
<quantity>2</quantity>
</a1:ShoppingCartItem>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Serializing Objects
• Guidelines for Serialization
When in doubt, mark a class as Serializable. Even if you do not need to serialize it
now, you might need serialization later. Or another developer might need to serialize a
derived class.
Serialized – This Event is raised just after serialization takes place. Apply OnSerialized
attribute to the method that should during this event.
Deserializing - This Event is raised just before deserialization takes place. Apply
OnDeserialized attribute to the method that should during this event
Deserialized - This Event is raised just after deserialization takes place and after
IDeserializationCallback.OnDeserialization has been called. We should use
IDeserializationCallback.OnDeserialization instead when the formatters other than BinaryFor.
Custom Serialization
• Change Serialization based on Context
Streamingcontext structure provides information about the destination of a serialized object
to class that implements the Iserializable interface. This structure contains two properties .
– After creating type object ,We can look at the methods, properties, events, interfaces and
inheritance tree of a particular type in the system.
– We have various Properties and Methods for Type Class like
Properties :
Assembly,AssembyQualifiedName, Attributes, BaseType, FullName, IsAbstract,
IsInterface, IsnotPublic, IsClass, IsEnum, IsSealed, IsValueType, IsVisible, Namespace,
IsPublic etc..
Methods :
GetConstructor, GetConstructors, GetEvent, GetEvents, GetField, GetFields,
GetInterface/s, GetMember/s, GetMethod/s, GetProperty, GetProperties etc..
Reflecting Types
• Enumerating Class Members
– Each part of a type is represented by a class within the
reflection system that ends with the name info.
– A number of methods on the Type class allow you to get each
of the parts of the type. These match the info classes as well.
– Example for retrieving properties all the properties of a class
Foreach(PropertyInfo prop in type.GetProperties)
{
// do the operations
}
Reflecting Types
• Understanding the MethodBody
– The MethodBody is a special sort of container that contains local variables and the actual
Intermediate Language (IL) instructions that are compiled into machine code at runtime.
– The local variable return as an array of LocalVariableInfo objects that contain information
about the type of each variable , finally we can get the array of bytes (actual IL code) that
the CLR uses to compile and run the m/c code.
AssemblyBuilder assemBldr=
AppDomain.CurrentDomain.DefineDynamicAssembly(tempName,AssemblyBuilderAccess.RunAndSave);
• Creating Members
– Fallowing is the various methods of the TypeBuilder that help us to define the members of a
type.
Define Constructor , DefineDefaultConstructor, DefineEvent, DefineField, DefineGenericParameters
DefineMethod, DefineProperty
– Sample
ConstructorBuilder ctorBldr =typeBldr.DefineDefaultConstructor(MethodAttributes.Public);
Creating Code at Runtime
– We can now take these constructor and create an ILGenerator object,
ILGenerator class is used to generate local variables and IL code
– Sample
ILGenerator codeGen = ctorBldr.GetILGenerator();
codeGen.Emit(OpCodes.Ret);
– Create a method by specifying its return type and parameter types like
MethodBuilder methBldr = typeBldr.DefineMethod("Add",MethodAttributes.Public,null,new
Type[] { typeof(string) });
– Define a static method by adding MethodAttributes.Static to the attributes like
MethodBuilder methBldr = typeBldr.DefineMethod("Add", MethodAttributes.Public |
MethodAttributes.Static,null,new Type[] { typeof(string) });
– Define a private field called _count like
FieldBuilder fieldBldr = typeBldr.DefineField(“_count”, typeof(int),
FieldAttrubutes.Private);
Creating Code at Runtime
– Define a property similar to how we define a method like
PropertyBuilder propBldr =
typeBldr.DefineProperty("Count",PropertyAttributes.None,typeof(int),Type.EmptyTypes);
– Method to perform set and get operation on property
MethodAttributes getAttributes =
MethodAttributes.Public |MethodAttributes.SpecialName |MethodAttributes.HideBySig;
MethodBuilder propGetBldr =
typeBldr.DefineMethod("get_Count",getAttributes,typeof(int),Type.EmptyTypes);
propBldr.SetGetMethod(propGetBldr);
– Persisting to Disk
assemBldr.Save("MyTempAssembly.dll");