tags:

views:

237

answers:

4

I'm trying to put together a fun 'contest' of sorts. Developers will write a bot that plays some game - maybe BlackJack and the master program will host the game and let the bots play against each other.

I've participated in such things before, but never been involved with the 'host' application. And I'm not sure how to go about doing that.

I'll be doing this in VB.Net

Different people will write their own bots - I'm guessing I'd want to require them to implement a particular interface I'll define. They'll compile it into a DLL and send that to me. I need to have the host call the same methods on each of the different 'bot' DLLs to progress the game play - but I'm having some trouble doing that.

What's the best way to do this?

+3  A: 

Load the assembly using the reflection API. Then iterate through the types in that assembly until you find one that inherits from some base class you've defined, or interface. Then create an instance of it and start calling the methods :-)

From the sample documentation:

Imports System
Imports System.Reflection

Class Class1
    Public Shared Sub Main()
        Dim SampleAssembly As [Assembly]
        ' You must supply a valid fully qualified assembly name here.            
        SampleAssembly = [Assembly].Load("Assembly text name, Version, Culture, PublicKeyToken")
        Dim Types As Type() = SampleAssembly.GetTypes()
        Dim oType As Type
        ' Display all the types contained in the specified assembly.
        For Each oType In Types
            Console.WriteLine(oType.Name.ToString())
        Next oType
    End Sub 'LoadSample
End Class 'Class1

Once you have the type, you can create the instance using the Activator

Joel Martinez
Why reinvent the wheel? There are many great DI frameworks that do this with lots of extra error checking, more complex features, etc.
Reed Copsey
sometimes one gains a lot of knowledge from reinventing the wheel. Either way, he asked how this could be done, and this is but one option. There are also other great answers in response to this thread :-)
Joel Martinez
+1  A: 

Ah, that idea brings back some memories from the very early days of .NET. I assume that code contains one or two bits of interest.

Fredrik Mörk
+1  A: 

Depending on how realistic you want to be, you could always leverage the MS Robotics Studio which ties into Visual Studio 2008 and includes a simulator. There's even a league.

Jacob Proffitt
+2  A: 

Take a look at the Managed Extensibility Framework. It would let you easily create an interface (or even base classes), and they could just add an Export attribute to their implementation. You'd get all of the discovery and injection for free.

Reed Copsey
This is for the moment the best clutter free way :) All the extensibility should be handled by a manager who knows how to do it, acording meta spex.
ruslander