Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reflection and Interfaces [2005]

Status
Not open for further replies.

DerPflug

Programmer
Mar 28, 2002
153
US
I have a situation where I have table that contains an id field and a class name field:

id class_name
1 ParseExcelFile.dll
2 ParseTextFile.dll

What I want to do is load one of these classes dynamically from a windows application based on the id I request from the database. I know that I can instantiate either one using reflection and CreateInstance. However, I want to use (I believe) an interface to reference a parsing routine in whatever class is instantiated. Something like this:

Code:
objAssembly = System.Reflection.Assembly.GetExecutingAssembly()
objParser = objAssembly.CreateInstance(strClassName)
lngStatus = objParser.FormatFile(strFileName)

My goal here is to be able to reference the method "FormatFile" despite whatever class I call. How do I implement an interface in this case? More specifically, do I compile a separate Class Library project just containing my interface code for the "FormatFile" function and then implement it in each of the parsing classes (as well as add it to the Reference section)? Or is my architecture altogether too complex? Thanks in advance.
 
Hello,

I'm not sure which is the best way to go but another option would be to create a base class something like the below with your FormatFile method defined as MustOverride. Then all of your parsing classes could inherit from it to provide the specific implementations. I also provided a FormatObject method in case the file was already open. Good Luck!
Code:
Public MustInherit Class ParsingBase

    Public MustOverride Sub FormatFile(ByVal path As String)

    Public MustOverride Sub FormatObject(ByVal file As Object)

End Class 'ParsingBase
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top