(.net) trying to do late-binding with specific items, seeing weird behavior (aka how to abuse .net) via /r/learnprogramming


(.net) trying to do late-binding with specific items, seeing weird behavior (aka how to abuse .net)

ok so this is long so bear with me
using .net 2 and vs2005 (don't ask)
One Solution, called SelfTest3, contains 3 projects:
MainPanel, ModuleClass, mConfiguration

ModuleClass, has Root namespace "SelfTest3" and Assembly name "ModuleClass". It Declares and defines an "abstract class" ModuleClass in file "ModuleClass.vb":

Imports SelfTest3 Public MustInherit Class ModuleClass Public sMagic As String Public sDescription As String Public MustOverride Function Foo() As Integer Public MustOverride Sub Bar() As Integer End Class 

mConfiguration has Root namespace "SelfTest3" and Assembly name "mconfiguration", it (tries to?) inherits ModuleClass to define the class mConfiguration

Public Class mConfiguration Inherits ModuleClass Public Shadows sMagic As String = "yes" Public Shadows sDescription As String = "configuration test" Public Overrides Function Foo() As Integer Return 1 End Function Public Overrides Sub Bar() As Integer End Function End Class 

MainPanel has also rootnamespace "SelfTest3" and assembly name "MainPanel" but this isn't important. It tries to create an instance of mConfiguration at runtime:

Imports System.Reflection Imports System.Runtime.InteropServices Imports System.Runtime.Remoting Imports SelfTest3 ' no idea why i need to import the project's solution namespace but... Dim oAssembly As Assembly = Assembly.LoadFrom("(Path to mConfiguration.dll)") Dim types As Type() = oAssembly.GetTypes() Dim iCandidate As Integer = -1 For iType As Integer = 0 To types.Length - 1 If types(iType).BaseType.Name = "ModuleClass" Then iCandidate = iType Exit For End If Next Dim mModule As ModuleClass = Activator.CreateInstance(types(iCandidate)) 

at this point, mModule is an instance of ModuleClass that contains an instance of mConfiguration :

http://ift.tt/2ucUpgL

why? why does mModule contain, as a separate member alongside its other members, the instance of mConfiguration that i asked to be created?

Submitted July 12, 2017 at 05:10AM by fwipyok
via reddit http://ift.tt/2sODptr

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s