locked
Create object with class name RRS feed

  • Question

  • User1699289794 posted

    Hello everybody,

    I am Rajesh. I have some problem in my project architecture. i.e I want to create objects with class name. 

    For example,

    I have 2 Components in my project

    i) BusinessObjects

    ii) Converter

    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.

    Eg. 

    <Employee>
       <EmpNo>1234</EmpNo>
       <Name>Rajesh</Name>
       <Salary>20000</Salary>
    </Employee>


     

    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)
                Response.Write(ic.ToString())


     

    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.

    Tuesday, March 23, 2010 1:18 AM

All replies

  • User-344996941 posted

    Provide the fully qualified name (including namespace) of the object in Type.GetType 
    e.g.

    t = Type.GetType("BusinessObject.Employee") 
    //namespace pattern:   YourCompanyNameSpace.ProductName.ComponentName.Employee


     

     

    Tuesday, March 23, 2010 2:44 AM
  • User1699289794 posted

    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.

    Class MyResponse
      Implements ICommon
        Private _info As MyInformation
        Public Property Information() As MyInformation
            Get
                Return _info
            End Get
            Set(ByVal value As MyInformation)
                _info = value
            End Set
        End Property
    
        Private _data As ICommon
        Public Property Data() As ICommon
            Get
                Return _data
            End Get
            Set(ByVal value As ICommon)
                _data = value
            End Set
        End Property
    End Class


     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()
            xs.Serialize(sw, res)
            Return sw.ToString()
    End Function

    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?

    Tuesday, March 23, 2010 3:33 AM