Type.GetType()

Browsing through ASP.NET Forums, I found an interesting questions raised by a member that when he tried to get type information of a class using Type.GetType it was a charm in C#, but VB did not worked in same manners. He was trying to create new instances of that class at runtime, and populate those instances with data fetched from any database.

I checked its documentation in MSDN, here are its salient points, which should always be thought out before using this.

It accepts only a parameter which must contain an assembly qualified name of required type. This parameter could be only type name (i.e. "Contact") or may include namespace in it (i.e. "Hansvits.Models.Contact").

If parameter has only type name then method will search only in the assembly in which that call was made. If any full/partial assembly name is mentioned then it will load that assembly and search in it.

Below is how I tested this behavior in both languages.

This is the C# version.

// Contact class with namespace
namespace Hansvits.Models
{
    public class Contact
    {
        private int _id;
        private string _name;

        public Contact()
        {
            _name = "";
            _id = 0;
        }

        public int Id
        {
            get { return _id; }
            set { _id = value; }
        }

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }
    }
}

public class Contact
{
    private int _id;
    private string _name;

    public Contact()
    {
        _name = "";
        _id = 0;
    }

    public int Id
    {
        get { return _id; }
        set { _id = value; }
    }

    public string Name
    {
        get { return _name; }
        set { _name = value; }
    }
}

// Getting type of above class
public class DataManager
{
    public DataManager()
    {
    }

    public ArrayList GetContacts(string type)
    {
        ArrayList items = new ArrayList();
        Type ct = Type.GetType(type);
        Object o = Activator.CreateInstance(ct);
        //get properties and load data in them
        items.Add(o);
        return items;
    }
}

DataManager m = new DataManager();
// without namespace
// it will fetch contact class which do not have any namespace defined.
m.GetContacts("Contact");
// with full namespace
// while this will fetch contact with its namespace
m.GetContacts("Hansvits.Models.Contact");

Same code in VB.Net

Namespace Hansvits.Models
    Public Class Contact
        Private _id As Integer
        Private _name As String

        Public Sub New()
            _id = 0
            _name = ""
        End Sub

        Public Property Id() As Integer
            Get
                Return _id
            End Get
            Set(ByVal value As Integer)
                _id = value
            End Set
        End Property

        Public Property Name() As String
            Get
                Return _name
            End Get
            Set(ByVal value As String)
                _name = value
            End Set
        End Property
    End Class
End Namespace

Public Class DataManager
    Public Function GetContacts(ByVal itemType As String) As ArrayList
        Dim items As New ArrayList()
        Dim ct As Type
        ct = Type.GetType(itemType)
        Dim o As Object = Activator.CreateInstance(ct)
        'load data in this object
        items.Add(o)
        Return items
    End Function
End Class

Dim manager As New DataManager()
' will return nothing in this case
manager.GetContacts("Contact")
' will return valid type
manager.GetContacts("Hansvits.Models.Contact")

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.