Convert the name of a type - to a typehttp://forums.asp.net/t/1371628.aspx/1?Convert+the+name+of+a+type+to+a+typeTue, 20 Jan 2009 09:06:40 -050013716282869014http://forums.asp.net/p/1371628/2869014.aspx/1?Convert+the+name+of+a+type+to+a+typeConvert the name of a type - to a type <p>Hi there,</p> <p>How can I use a string parameter for a method and convert this to a type. I am using reflection to retrieve items within an enumeration:</p> <p><span class="kwd">Dim</span> fi <span class="kwd">As</span> FieldInfo() = <span class="kwd"> GetType</span>(VehicleContractType).GetFields </p> <p>Currently I am creatnig a separate method for each&nbsp;enumeration in order to retrieve it's values.</p> <p>What I would like to do is pass the name of the&nbsp;enumeration as a parameter to the method and cast&nbsp;this as a type.</p> <p>Any ideas?</p> <p>Thanks,</p> <p>Ben&nbsp;</p> <p>&nbsp;</p> 2009-01-15T14:12:22-05:002869029http://forums.asp.net/p/1371628/2869029.aspx/1?Re+Convert+the+name+of+a+type+to+a+typeRe: Convert the name of a type - to a type <p>&nbsp;Try:</p> <blockquote> <p><span id="intelliTxt">Type.GetType(&quot;MyClass&quot;)</span><br> </p> </blockquote> 2009-01-15T14:22:13-05:002870932http://forums.asp.net/p/1371628/2870932.aspx/1?Re+Convert+the+name+of+a+type+to+a+typeRe: Convert the name of a type - to a type <p>This is what I tried:</p> <p>&nbsp;<pre class="prettyprint">Public Shared Function GetEnumTypes(ByVal enumName As String) As List(Of EnumObject) Dim recordset As New List(Of EnumObject) Dim s As Type = Type.GetType(enumName) Dim fi As FieldInfo() = s.GetFields For Each f As FieldInfo In fi If Not f.IsSpecialName AndAlso Not (f.Name.ToLower = &quot;notset&quot;) Then Dim myValue As Integer = CType(f.GetValue(0), Integer) recordset.Add(New EnumObject(myValue, f.Name)) End If Next Return recordset End Function</pre> <P>&nbsp;But "s" returns nothing. I am passing in the name of the enumeration. An example below:</P><pre class="prettyprint"> <SPAN class=kwd>Dim</SPAN> enums <SPAN class=kwd>As</SPAN> List(Of EnumObject) = EnumObject.GetEnumTypes(<SPAN class=st>"VehicleStatusType"</SPAN>) <SPAN class=kwd>For Each</SPAN> en <SPAN class=kwd>As</SPAN> EnumObject <SPAN class=kwd>In</SPAN> enums lbl1.Text &amp;= en.Name <SPAN class=kwd>Next</SPAN></pre> </p> <p>&nbsp;Thanks,</p> <p>Ben</p> 2009-01-16T10:12:09-05:002874198http://forums.asp.net/p/1371628/2874198.aspx/1?Re+Convert+the+name+of+a+type+to+a+typeRe: Convert the name of a type - to a type <p>&nbsp;If I understand what you are trying to do, you want to use GetType to load the type of an Enum. Try including the name of the assembly. Here is an example of an assembly named &quot;Test&quot;</p> <blockquote>public Enum food<br> &nbsp;&nbsp;&nbsp; pizza<br> &nbsp;&nbsp;&nbsp; chocolate<br> End Enum<br> <br> Public Class Form1<br> <br> &nbsp;&nbsp;&nbsp; Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load<br> <br> <b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim foodType As Type = Type.GetType(&quot;Test.food&quot;)</b><br> <br> &nbsp;&nbsp;&nbsp; End Sub<br> <br> End Class<br> </blockquote> <p>&nbsp;</p> 2009-01-18T08:14:28-05:002878755http://forums.asp.net/p/1371628/2878755.aspx/1?Re+Convert+the+name+of+a+type+to+a+typeRe: Convert the name of a type - to a type <p>I was not including the namespace.</p> <p>It works now using Type.GetType so thank you.</p> <p>Ben</p> 2009-01-20T09:06:40-05:00