Click here to Skip to main content
Licence 
First Posted 23 Apr 2004
Views 29,387
Bookmarked 10 times

Using GetAssemblyInfo to locate and instantiate a variable, using Reflection

By rj45 | 23 Apr 2004
Use GetAssembly(), InvokeMember() and GetFields() to mine an assembly.
6 votes, 60.0%
1
2 votes, 20.0%
2

3

4
2 votes, 20.0%
5
1.56/5 - 10 votes
μ 1.56, σa 2.86 [?]

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
        ' Set the Type instance to the target class type.
        typeInt = Integer1.GetType()
        ' Instantiate an Assembly class to the assembly housing the Integer type.  
        SampleAssembly = [Assembly].GetAssembly(Integer1.GetType())
        ' Gets the location of the assembly using file: protocol.
        'Console.WriteLine(("CodeBase=" + SampleAssembly.CodeBase))
        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 Function
End Class 'AssemblyName_GetAssemblyName

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("___________________________" &_ 
            "____________________________________________") '40
        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 Sub
End Module

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

rj45

Software Developer (Senior)

Canada Canada

Member


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 2 PinmemberMark Hurd15:07 27 Jan '10  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120210.1 | Last Updated 24 Apr 2004
Article Copyright 2004 by rj45
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid