It's usual to create a XML Document using some information created dynamically.
Usually we have a type that have one or more properties (or fields) were stored
all information. Creating a simple XML accessing to all properties or fields is
a loss of time. Eventually, some types have a specific XML element. In those
cases, we must manually generate that element, but only those elements. But
using reflection we can create xml by accessing properties of an object.
Reflection is mechanism of discovering class information solely at run time.
Using reflection you would see all the properties, functions, events,
constructor of an object.
It defines an assembly which is a reusable, version able and self describing
building block of a Common Language Run time application. I will discuss here
only the functions that I have used in my code.
objAssembly = System.Reflection.Assembly.LoadFrom(str);
LoadFrom function takes the path of the assembly to load and loads it and
returns that Assembly.
After loading the assembly we want to get all the types in that assembly.This
class provides a function GetTypes() (It returns an array of System.Type objects
in that assembly) which will do that for us and that line for the code is.
arOfTypes = objAssembly.GetTypes();
Now having touched System.Type class,Let us talk about it.
This class represents type declarations.(class types, interface types, array
types, value types and enumeration types.)
It is the root of all Reflection operations.It is the primary means by which we
access metadata and it acts as a Gateway to Reflection API.It provides methods
for obtaining information about a Type declaration, such as the constructors,
properties, methods and Events.
Extracting Constructor Information:
It is an object of type System.Type. This method returns an array of objects of
type ConstructorInfo. This calss is part of System.Reflection Name space.
Extracting Properties Information:
PropertyInfo pInfo = t.GetProperties() ;
//It is an object of type System.Type. This method returns an array of objects
of type PropertyInfo. This calss is part of System.Reflection Name space.
Extracting Method Information:
Extracting Event Information:
Once I get all this info,I am presenting it in a tree view control.This should
fairly give a good idea of getting information about a Type.
Once you know all the information about a class, what do you want to do?
Probably you want to create an instance of that class and execute one of its
methods. I have created two classes MyClass1 and MyClass2 as part of this name
space and in the click event of more reflection button I have created an
instance of each of these classes and invoked its methods. I have put message
boxes so that we would know that they are getting executed. Let us look at this
Type t= Type.GetType("MyReflection.MyClass1");
I have passed the name of the class to static method GetType of Type class which
would return me a type Object of type MyClass1.Once you get Type object, as
mentioned before you could do lot of things with it. Let us look at Activator
class and see what we can do with this class and Type class.
In this simple case, we have a public class were allocated the information. The
class looks like following:
public class Identification
_name = null;
_address = null;
_name = value;
_address = value;
Next we create an instance of Identification class.
identification = new
identification.Name = "Edgar";
identification.Address = "Lisbon";
Finally create XML Document using reflection to access properties of our
xmlDoc = new
XmlDeclaration xmlDec =
XmlElement elemRoot =
XmlElement elem =
Type idType = identification.GetType();
foreach (PropertyInfo pInfo
object o =
elem = xmlDoc.CreateElement(pInfo.Name);
elem.InnerText = o.ToString();
In this article we have seen that how reflection is helpful for creating xml
Is this the Work Around or Best Solution?
Yes this is Best Solution.