jmac82

Hello.
This is the first time I post a message here, so i hope I am doing it right, and in the right place
I am new to vb.net.

I am making an application that gets some data from a website. I created a class, with a set of functions witch reads these websites returns the data i want.
Since these websites present their data in different ways, i have to make a class for each website.

My idea is that i would make a separate class library for each. (DLL files). So if my application in the future was to support a new website, the user could (for example) just add a new dll file to a specific folder, and maybe add some information to the registry, if needed. So when the application loads, it reads the contents of this folder, and the user gets a list of witch sites the application supports. Almost like plugins..

I've managed to create a ClassLibrary DLL and add a reference to it in my project, and so on..
But how do i add references to this list of dll-files in runtime






Re: Visual Basic Language Adding references at runtime

MS Johan Stenberg

http://msdn2.microsoft.com/en-us/library/25y1ya39.aspx gives an example on how to load an assembly at runtime. For the next version of Visual Studio/.NET framework, the CLR team will provide more support for add-ins - check out http://blogs.msdn.com/clraddins/ for more info.

Best regards,

Johan Stenberg






Re: Visual Basic Language Adding references at runtime

hgen_banks

To provide a little more detail, what you will probably want to do is create one or more interfaces that you will call to get the information from each website. This interface will include all the methods you need to download the information, parse it, and present it to the application in a way that you can use. Also, if you have some functions that will be used across most of the "extensions", you can create a base class that provides the common functionality (such as getting the web page) and implements the interface through abstract (MustOverride) methods. Any interfaces and base classes that will be referenced by the extensions should be placed in a separate assembly from the main program so that the extensions can reference an assembly with just the classes/interfaces they need. Then in your extension dll, you can reference the interface dll, inherit from the base class, and fill in the implementation details for that website.

Now that you have the extensions, you will need to load them into your program. Here is some sample code based on a system I have implemented. (At this point, the assemblies have been read into a dictionary(of String, Assembly) call kv. You should be able to get the list of assemblies in the application folder yourself.)


For Each t As Type In kv.Value.GetExportedTypes
'see if the class implements one of the interfaces
If t.GetInterface("ProjectName.Core.IExtension", True) IsNot Nothing OrElse _
t.GetInterface("ProjectName.Core.IVisualExtension", True) IsNot Nothing Then
extensions.Add(t)
ElseIf t.IsSubclassOf(GetType(Packets.ExtendedPacket)) Then
'register the packet as an extended packet
Packets.ExtendedPacket.RegisterExtendedPacket(t)
'check to see if a renderer was provided with the packet
rendType = Type.GetType(Assembly.CreateQualifiedName(t.Assembly.FullName, _
t.FullName.Replace("Packets.", "Renderers.") & "Renderer"))

If rendType IsNot Nothing AndAlso _
rendType.GetInterface("MicroCommEmulator.Core.IPacketRenderer") IsNot Nothing Then
PacketListBox.AddRenderer(t, _
CType(Activator.CreateInstance(rendType), IPacketRenderer))

End If
End If
Next

The main things to see there are the use of Type.GetInterface, which tells you if a type implements a given interface, and Type.IsSubclassOf, which as named tells you if a type is a subclass of another type. Either of these could be used depending on how you want to implement extensions. Also of not is the call to Activator.CreateInstance near the end. You will need to do this since you can not directly call New because this code does not even know what type it is creating.

Once you have created the instance of the extension, you should keep it in a list that you can use to find the appropriate extension and then execute the methods you will need. In your case I would suggest a Dictionary(of String, IExtension) where the key is the website the extension supports and the value is an instance of the extension you will use to process the data.

Extensibility is a fun and useful thing to be able to add to a project, just do not be afraid of getting lots of TargetInvocationExceptions when you are debugging. The exception itself is incredibly useless except for the InnerException property that can provide the information about where the exception really occurred.

Good luck!!




Re: Visual Basic Language Adding references at runtime

jmac82

Thank you very much a excellent reply!
This is exactly what I was looking for.