How to get access to the functions from a module - vb.net

I need to get access to the public functions in a module (not a class). This is what I have tried:
Dim asm As Reflection.Assembly = Assembly.GetExecutingAssembly
Dim my_modules = asm.GetModules
For Each my_module In my_modules
Dim all_methods = my_module.GetMethods
Stop
Next
But that doesn't even get down to the module itself, I just get the name of the executable.

As #jmcilhinney said in the comments a Module is like a Class when using reflection. You can access it using GetType or GetTypes method.
Dim asm As Assembly = Assembly.GetExecutingAssembly
Dim my_module = asm.GetType("Module_Full_Name")
Dim allMethods = my_module.GetMethods()

Module in VB.NET is nothing else than static class. You not create instance of static class, just call its members from anywhere in scope where module is declared.
Public Module Mathematics
Public Function Sum(x As Integer, y As Integer) As Integer
Return x + y
End Function
End Module
Class Form1
Public Sub New()
InitializeComponent()
Dim result = Mathematics.Sum(1, 2)
End Sub
End Class

Related

Assigning class Instance to Interface

Have two questions regarding the code below:
In the following code, the class Test implement the interface Test_Interafce. I have read in more than one articles that when a class implements an interface, we should assign the class instance to the interface. Going by that in the following code the statement Dim t As New Test() should be replaced by Dim t As Test_Interface = New Test(). However I do not understand the advantage or need for the same. When I instantiate the class Test by simply writing Dim t As New Test(), I am able to access all elements (even the procedures of the interface implemented by the class) through the instance "t" and the code seems to be working fine. So then why to assign a class instance to an interface?
Infact if I write Dim t As Test_Interface = New Test() then through "t" does not allow me to access the subroutine CHECK in the class Test. The error being displayed is: "CHECK is not a member of Test_Interface". So isnt that a disadvantage??!!
What is the use of the statement: Throw New NotImplementedException(). This statement comes automatically when I implement teh interface in a class/structure.
The code is found below:
Module Module1
Interface Test_Interface
Function Length(ByVal s As String) As Integer
Sub Details(ByVal age As Integer, ByVal name As String)
End Interface
Sub Main()
Dim t As New Test() 'Alternate definition: Dim t As Test_Interface = New Test()
t.Details(31, "Mounisha")
Console.WriteLine("Length of the string entered = {0}", t.Length("Hello"))
t.check()
Console.ReadLine()
End Sub
End Module
Class Test
Implements Test_Interface
Public Sub Details(age As Integer, name As String) Implements Test_Interface.Details
Console.WriteLine("Age of person = {0}", age)
Console.WriteLine("Name of person = {0}", name)
'Throw New NotImplementedException() ----> what does this do?
End Sub
Public Function Length(s As String) As Integer Implements Test_Interface.Length
Console.WriteLine("Original String: {0}", s)
Return s.Length()
'Throw New NotImplementedException()
End Function
Sub check()
Console.WriteLine("The sub can be accessed")
End Sub
End Class

VB.Net making extension method without polluting global namespace?

To write a extension method in vb.net you have to put them in a Module, all methods defined in a module get added to the global namespace.
Is there any way to remove the methods from the global namespace so they only are used as extension method or if you actually write the module name first?
Example
TypeExtensionMethods.vb:
Imports System.Runtime.CompilerServices
Public Module TypeExtensionMethods
Private ReadOnly numericTypes As TypeCode() = {
TypeCode.Byte, TypeCode.SByte, TypeCode.UInt16,
TypeCode.UInt32, TypeCode.UInt64, TypeCode.Int16,
TypeCode.Int32, TypeCode.Int64, TypeCode.Decimal,
TypeCode.Double, TypeCode.Single}
<Extension()>
Public Function IsNumber(type As Type) As Boolean
Dim currentTypeCode As TypeCode = Type.GetTypeCode(type)
Return currentTypeCode.IsNumber()
End Function
<Extension()>
Public Function IsNumber(type As TypeCode) As Boolean
Return numericTypes.Any(Function(tc) tc = type)
End Function
End Module
Class1.vb:
Class Class1
Sub Main(value as Object)
'This should work
dim result1 as Boolean = value.GetType().IsNumber()
'This should work
dim result2 as Boolean = TypeExtensionMethods.IsNumber(value.GetType())
'This should not work, the method IsNumber is polluting as it can be called without specifying where its defined.
dim result3 as Boolean = IsNumber(value.GetType())
End Sub
End Class
It seems this is not possible!

VB.NET Plugin Architecture

Hello I'm working on using the interface class in vb.net to make a plugin architecture. So far I've not found a step by step tutorial for a beginner. But I've gotten as far as this:
Main App
Public Class PluginHandler
Interface IApplications
Sub ChangeForms()
End Interface
Public Shared Sub GetDLLFromDir(ByVal TheDir)
For Each dll As String In System.IO.Directory.GetFiles(TheDir, "*.dll")
LoadPlugin(dll)
Next
End Sub
Public Shared Sub LoadPlugin(ByVal ThePlugin)
Dim Asm = Assembly.LoadFile(ThePlugin)
Dim type As Type = Asm.GetType("TestPlugin.Class_TestPlugin")
Dim method As MethodInfo = type.GetMethod("ChangeForms")
method.Invoke(Nothing, Nothing)
End Sub
End Class
TestPlugin.vb
Public Class Class_TestPlugin
Implements Plugin_Application.PluginHandler.IApplications
Sub ChangeForms() Implements Plugin_Application. _
PluginHandler.IApplications.ChangeForms
Dim NewForm As New Form_Test
NewForm.Show()
End Sub
End Class
My issue is it says for the method to invoke --> Non-static method requires a target.
I seen on another forum that the method may not be found. I found it says the methods name and void. But I'm not sure what to do. If somebody can mod my code to work or give me some ideas to make my code work. Thanks :)
Here is a link to my test project folder: Link
You have to create an object the method should be called on:
Dim ctor = type.GetConstructor({}) 'no parameters for constructor
Dim obj = ctor.Invoke({})
method.Invoke(obj, {})

vb.net dynamic plugin loading question

I'm using the following code in a vb.net 2008 project to dynamically load dll's (with forms) from a folder, all works great however I can't figure out for the life of me how to call a function or get a public variable from the plugins.
Can anyone answer this issue for me?
Dim PluginList As String() = Directory.GetFiles(appDir, "*.dll")
For Each Plugin As String In PluginList
Dim Asm As Assembly
Dim SysTypes As System.Type
Asm = Assembly.LoadFrom(Plugin)
SysTypes = Asm.GetType(Asm.GetName.Name + ".frmMain")
Dim IsForm As Boolean = GetType(Form).IsAssignableFrom(SysTypes)
If IsForm Then
Dim tmpForm As Form = CType(Activator.CreateInstance(SysTypes), Form)
You should probably create an interface in a common assembly and have your form implement it, this way you can cast you dynamically loaded object as your interface type.
Imports System.Reflection
Imports Plugin.Interfaces
Sub Main()
Dim assembly As Assembly
assembly = assembly.LoadFrom("Plugin.X.dll")
Dim type As Type
Dim found As Boolean = False
For Each type In assembly.GetTypes()
If GetType(IForm).IsAssignableFrom(type) Then
found = True
Exit For
End If
Next
If found Then
Dim instance As IForm
instance = CType(Activator.CreateInstance(type), IForm)
Console.WriteLine(instance.Add(20, 20))
End If
End Sub
Interface Assembly
Public Interface IForm
Function Add(ByVal x As Integer, ByVal y As Integer) As Integer
End Interface
Plugin Assembly
Imports Plugin.Interfaces
Public Class Form
Implements IForm
Public Function Add(ByVal x As Integer, ByVal y As Integer) As Integer Implements IForm.Add
Return x + y
End Function
End Class

Implementing my own interface in VBA - Error: Object module needs to implement 'x' for interface 'y'

How do I implement my class ClsInterface, which has this code:
Public Function add(x As Integer, y As Integer) As Integer
End Function
in my class Class2, which has this code:
Implements ClsInterface
Public Function add(x As Integer, y As Integer) As Integer
add = x + y
End Function
My test code is
Public Sub test()
Dim obj As New Class2
MsgBox obj.add(5, 2)
End Sub
This always comes up with the following error:
Microsoft Visual Basic
Compile error:
Object module needs to implement 'add' for interface 'ClsInterface'
OK/Help
but there is no help on Microsoft help (when I press on Help button).
Any Ideas?
Your Class2 must look like:
Implements ClsInterface
Private Function ClsInterface_add(x As Integer, y As Integer) As Integer
ClsInterface_add = x + y
End Function
Check out the drop-down boxes at the top of Class2's code window, you can see what base object you can refer to; Class or ClsInterface.
In your test code you want:
Dim obj As New ClsInterface
If you want to call across the interface.
I would also recommend naming interfaces in the form ISomeDescription and using Dim then Set rather than Dim As New.