473,398 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Converting object to an interface

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("AuthenticationP rovider"))
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.
Nov 18 '05 #1
6 1340
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("AuthenticationP rovider"))
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

--
Matt Berther
http://www.mattberther.com
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("AuthenticationP rovider"))
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.

Nov 18 '05 #2
Hello Martin,

I think I misunderstood your problem...

How about instead of

Return objPlugin

do

Return CType(objPlugin, AuthenticationInterface)

--
Matt Berther
http://www.mattberther.com
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("AuthenticationP rovider"))
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.

Nov 18 '05 #3
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.

Nov 18 '05 #4
Hello mc******@gmail.com,

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

--
Matt Berther
http://www.mattberther.com
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.

Nov 18 '05 #5
I have.... I get it when trying to convert the output of that function
into the interface

Nov 18 '05 #6
Hello mc******@gmail.com,

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.

--
Matt Berther
http://www.mattberther.com
I have.... I get it when trying to convert the output of that function
into the interface

Nov 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: matt melton | last post by:
Hi there, I am trying to write a method that accepts an array of any primitive type and will return the same array without copying memory as an array of bytes. ie. I'd like to be able to do...
3
by: masood.iqbal | last post by:
In this day and age, you never say no to any work that is thrown at you ---- so when I was offered this short-term contract to convert legacy C code to C++, I did not say no. Personally I believed...
3
by: DDE | last post by:
Hi all, I have defined a meththod supposed to do some treatment with the class it receives as argument. This method can receive different type of classes so it's argument is defined as an...
8
by: iyuen | last post by:
I'm having problems with converting a byte array to an image object~ My byte array is an picture in VB6 StdPicture format. I've used propertybag to convert the picture into base64Array format in...
11
by: Eric | last post by:
hi, I want to convert a C# class into COM, so that I can use the class in C++. The codes compile and link well. But when I run the program, I got a exception. Any comment is welcome. Thanks in...
9
by: Terry | last post by:
I am converting (attempting) some vb6 code that makes vast use of interfaces. One of the major uses is to be able to split out Read-only access to an obect. Let me give you a simple (contrived)...
7
by: Coleen | last post by:
Does anyone have any good detailed information on the conversion process? We are in the process of converting 2 projects from 2003 to 2005 and have some conversion errors that I can not find...
2
by: TheLongshot | last post by:
Ok, let's try this again. I have an ASP.NET 1.1 application that I'm working to convert to 2.0, but I've run into a snag. The problem is with this line: return...
5
by: vtjumper | last post by:
I'm building a C# interface to an existing messaging system. The messaging system allows values of several types to be sent/recieved over the interface. What I want to do is use a generic...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.