How can I create a generic constructor? (ie. BaseClass.FromXml(<param>)

Posted by SofaKng on Stack Overflow See other posts from Stack Overflow or by SofaKng
Published on 2010-03-19T14:37:40Z Indexed on 2010/03/19 14:41 UTC
Read the original article Hit count: 226

I'm not sure how to describe this but I'm trying to create a base class that contains a shared (factory) function called FromXml. I want this function to instantiate an object of the proper type and then fill it via an XmlDocument.

For example, let's say I have something like this:

Public Class XmlObject
    Public Shared Function FromXml(ByVal source as XmlDocument) As XmlObject
        // <need code to create SPECIFIC TYPE of object and return it
    End Function
End Class

Public Class CustomObject
    Inherits XmlObject
End Class

I'd like to be able to do something like this:

Dim myObject As CustomObject = CustomObject.FromXml(source)

Is this possible?

© Stack Overflow or respective owner

Related posts about vb.net

Related posts about constructors