Introduction
How to use reflection to find out all the types in an assembly.
First off, get the assembly from type integer. This should get the mscorlib.dll. (Main .NET assembly).
SampleAssembly = [Assembly].GetAssembly(Integer1.GetType())
Now, get the types from it:
types = SampleAssembly.GetTypes()
Now, get an object from the types array.
obj = types(i).InvokeMember(Nothing, _
BindingFlags.DeclaredOnly Or BindingFlags.Public_
Or BindingFlags.NonPublic Or BindingFlags.Instance_
Or BindingFlags.CreateInstance, Nothing,_
types(i), Nothing)
To get all of the Fields (Public variables), call GetFields. (However, you could call GetProperties or GetMethods which work very similar.)
fields = obj.GetType().GetFields((BindingFlags.Public Or BindingFlags.Static))
To get a value from a field, use .GetValue from the field. (You may not want to cast to string.) (CType is VB's ugly way of casting)
strResult = CType(fields(i2).GetValue(Nothing), String)
The Code
Option Explicit On
Imports System
Imports System.Reflection
Imports System.Text.RegularExpressions
Imports Microsoft.VisualBasic
Public Class GetAssemblyInfo
Public Class RequiredInfo
Public strDataType As String
Public strSize As String
Public strMin As String
Public strMax As String
End Class
Public Function getInfo(ByVal strType As String) As RequiredInfo
Dim SampleAssembly As [Assembly]
Dim Integer1 As New Int32
Dim typeInt As Type
Dim types() As Type
Dim obj As [Object]
Dim fields As FieldInfo()
Dim intfieldValue As Integer
Dim fltfieldValue As Single
Dim strResult As String Dim rqInfo As New RequiredInfo
typeInt = Integer1.GetType()
SampleAssembly = [Assembly].GetAssembly(Integer1.GetType())
types = SampleAssembly.GetTypes()
Dim strTypeDesc As String
For i As Integer = 0 To types.Length - 1
If (types(i).Name() = strType) Then
rqInfo.strDataType = types(i).Name()
obj = types(i).InvokeMember(Nothing, _
BindingFlags.DeclaredOnly _
Or BindingFlags.Public _
Or BindingFlags.NonPublic Or _
BindingFlags.Instance Or _
BindingFlags.CreateInstance, _
Nothing, types(i), Nothing)
fields = obj.GetType().GetFields((BindingFlags.Public_
Or BindingFlags.Static))
For i2 As Integer = 0 To fields.Length - 1
If (fields(i2).Name = "MaxValue") Then
strResult = CType(fields(i2).GetValue(Nothing), String)
rqInfo.strMax = strResult End If
If (fields(i2).Name = "MinValue") Then
strResult = CType(fields(i2).GetValue(Nothing), String)
rqInfo.strMin = strResult
End If
Next i2
rqInfo.strSize = Len(obj)
Return rqInfo
End If
Next
End FunctionEnd Class
Here is the Module
Option Explicit On
Module modCallAssemblyInfo
Sub Main()
Dim getAssInfo As New GetAssemblyInfo Dim reqInfo As GetAssemblyInfo.RequiredInfo = getAssInfo.getInfo("Single")
Dim InitReqInfo As New GetAssemblyInfo.RequiredInfo InitReqInfo.strDataType = "Data Type"
InitReqInfo.strMax = "MaxValue"
InitReqInfo.strMin = "MinValue"
InitReqInfo.strSize = "Size (Bytes)" Console.WriteLine("___________________________" &_
"____________________________________________") Write(InitReqInfo)
Write(getAssInfo.getInfo("Int16"))
Write(getAssInfo.getInfo("Int32"))
Write(getAssInfo.getInfo("Int64"))
Write(getAssInfo.getInfo("Single"))
Write(getAssInfo.getInfo("Double"))
End Sub Public Sub Write(ByRef info As GetAssemblyInfo.RequiredInfo)
Console.WriteLine("{0,-15}|{1,-15}|{2,-25}|{3,-20}",_
info.strDataType, info.strSize,_
info.strMin, info.strMax)
End SubEnd Module