Converting object to an interface

M

Martin Carolan

Hi there,

I have an interface (called AuthenticationInterface) shown below:

Public Interface AuthenticationInterface
Function IsLoggedIn() As Boolean
Function Authenticate(ByVal User As LinkDirectoryUser) As Boolean
Function GetCurrentUser() As LinkDirectoryUser
Function IsUserSuspended(ByVal User As LinkDirectoryUser) As
Boolean
Function GetAllRoles() As String()
Sub Register(ByVal User As LinkDirectoryUser)
Sub Logout()
Sub DeleteUser(ByVal User As LinkDirectoryUser)
Sub SuspendUser(ByVal User As LinkDirectoryUser)
Sub UpdateUser(ByVal User As LinkDirectoryUser)
End Interface

And I use the following code to load a type that implements that
interface based on the value of the web.config file:

Public Function Current() As Object
Dim asm As String = HttpContext.Current.Server.MapPath("bin\"
& ConfigurationSettings.AppSettings("AuthenticationProvider"))
Dim objDll As [Assembly] = [Assembly].LoadFrom(asm)

For Each t As Type In objDll.GetTypes
If t.IsPublic Then
If Not ((t.Attributes And TypeAttributes.Abstract) =
TypeAttributes.Abstract) Then
Dim objInterface As Type =
t.GetInterface("AuthenticationInterface", True)

If Not objInterface Is Nothing Then
Dim objPlugin As Object =
objDll.CreateInstance(t.FullName)
Return objPlugin
End If

End If
End If
Next
End Function

As you can see this returns an object, this is because I get casting
errors when trying to convert the object to a AuthenticationInterface
(but I can still use the object perfectly), my question is how can I
convert the object loaded to a AuthenticationInterface without causing
a cast error?

Thanks loads, Martin.
 
M

Matt Berther

Hello Martin,

I'm not familiar with VB, but Ill take a stab at this...

Public Function Current() As AuthenticationInterface
Dim asmPath As String = HttpContext.Current.Server.MapPath("bin\" & ConfigurationSettings.AppSettings("AuthenticationProvider"))
Dim asm As [Assembly] = [Assembly].LoadFrom(asmPath)

For Each t As Type in asm.GetExportedTypes()
If t.IsAssignableFrom(TypeOf(AuthenticationInterface)) ' This may
be TypeOf(AuthenticationInterface).IsAssignableFrom(t)
Return Activator.CreateInstance(t)
End If
Next

End Function
 
M

Matt Berther

Hello Martin,

I think I misunderstood your problem...

How about instead of

Return objPlugin

do

Return CType(objPlugin, AuthenticationInterface)
 
M

mcarolan

Sorry if I confused you, I get a casting error:

Specified cast is not valid.
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.

Exception Details: System.InvalidCastException: Specified cast is not
valid.

When trying to convert it into the interface, Thanks for the help,
martin.
 
M

Matt Berther

Hello (e-mail address removed),

Can you post the exact snippet of code that causes this error?
 
M

mcarolan

I have.... I get it when trying to convert the output of that function
into the interface
 
M

Matt Berther

Hello (e-mail address removed),

I imagine that using your posted code, the code that generates the cast error
looks something like this?

Dim myObj As Object = someClass.Current()
Dim authInterface As AuthenticationInterface = CType(myObj, AuthenticationInterface)

This should work... If not, you might look at DirectCast instead of CType.

In C#, you would do:

AuthenticationInterface authInterface = (AuthenticationInterface)someClass.Current();

and I believe the equivalent to that is CType.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top