Last post Mar 23, 2010 03:33 AM by RajeshKalis
Mar 23, 2010 01:18 AM|RajeshKalis|LINK
I am Rajesh. I have some problem in my project architecture. i.e I want to create objects with class name.
I have 2 Components in my project
i) BusinessObjects: (ClassLibrary)
In BusinessObjects component i have to write classes with some properties and methods. These classes should be implement ICommon interface (null interface).
It is ok cleared.
ii) Converter: (ClassLibrary)
In Converter component i have one class called XmlConverter, it is just duplicate of XmlSerializer. It contains one method called Deserialize(xml_string). Then I want to create object and return this object as ICommon reference. Here I will send data as
xml format. From this xml i want to create object. I will send class name as xml tag.
I will send this to XmlSerializer object xs.Deserialize(xml). Then it should return Employee object as ICommon reference.
I have tried, But it shows an error.
Dim t As Type
t = Type.GetType("Employee") ' here it returns null into t.
Dim obj As Object
obj = Activator.CreateInstance(t)
'obj = Activator.CreateInstanceFrom(Server.MapPath("~/Bin"), "Employee")
Dim ic As ICommon
ic = DirectCast(obj, ICommon)
Employee class designed in BusinessObjects component. The above code is written in Converter component. I will use these two component in my main project.
How can i use Activator.CreateInstance(), even the classes are in different class libraries.
Mar 23, 2010 02:44 AM|binoj7|LINK
Provide the fully qualified name (including namespace) of the object in Type.GetType
t = Type.GetType("BusinessObject.Employee")
//namespace pattern: YourCompanyNameSpace.ProductName.ComponentName.Employee
Mar 23, 2010 03:33 AM|RajeshKalis|LINK
I have done. but again i faced other problem.
i.e converting Response object to XML.
I will explain that problem.
same components i have mentioned above are contain some other methods to create and convert MyResponse object and xml.
i mean, I have one Extra Class called MyResponse.
Private _info As MyInformation
Public Property Information() As MyInformation
Set(ByVal value As MyInformation)
_info = value
Private _data As ICommon
Public Property Data() As ICommon
Set(ByVal value As ICommon)
_data = value
I will create this object in one of the business layer class and returns to interaction layer.
MyResponse object contain two references for two different classes MyInformation and ICommon.
MyInformation class will hold information of the result, for eg: messeges like failed, succeded...
ICommon is an interface it will hold data as object. Which is depends on result. i.e Either Employee or Student or any other class object which is implemented from ICommon.
When I try to convert this MyResponse object to xml by using XmlSerializer.
It shows an Exception: System.InvalidOperationException
Message:There was an error reflecting type 'SpiceBusinessObjects.SpiceResponse'.
Public Function Serialize(ByVal res As MyResponse) As String
Dim t As Type
t = res.GetType()
Dim xs As New XmlSerializer(t) ' here it given error
Dim sw As New StringWriter()
I think error is because i am using Interface(ICommon) in MyResponse.
My question is.
How to serialize an object to xml format, if that object class contains any interface references?