473,624 Members | 2,615 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Converting object to an interface

Hi there,

I have an interface (called AuthenticationI nterface) shown below:

Public Interface AuthenticationI nterface
Function IsLoggedIn() As Boolean
Function Authenticate(By Val User As LinkDirectoryUs er) As Boolean
Function GetCurrentUser( ) As LinkDirectoryUs er
Function IsUserSuspended (ByVal User As LinkDirectoryUs er) As
Boolean
Function GetAllRoles() As String()
Sub Register(ByVal User As LinkDirectoryUs er)
Sub Logout()
Sub DeleteUser(ByVa l User As LinkDirectoryUs er)
Sub SuspendUser(ByV al User As LinkDirectoryUs er)
Sub UpdateUser(ByVa l User As LinkDirectoryUs er)
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.Cur rent.Server.Map Path("bin\"
& ConfigurationSe ttings.AppSetti ngs("Authentica tionProvider"))
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( "Authentication Interface", True)

If Not objInterface Is Nothing Then
Dim objPlugin As Object =
objDll.CreateIn stance(t.FullNa me)
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 AuthenticationI nterface
(but I can still use the object perfectly), my question is how can I
convert the object loaded to a AuthenticationI nterface without causing
a cast error?

Thanks loads, Martin.
Nov 18 '05 #1
6 1348
Hello Martin,

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

Public Function Current() As AuthenticationI nterface
Dim asmPath As String = HttpContext.Cur rent.Server.Map Path("bin\" & ConfigurationSe ttings.AppSetti ngs("Authentica tionProvider"))
Dim asm As [Assembly] = [Assembly].LoadFrom(asmPa th)

For Each t As Type in asm.GetExported Types()
If t.IsAssignableF rom(TypeOf(Auth enticationInter face)) ' This may
be TypeOf(Authenti cationInterface ).IsAssignableF rom(t)
Return Activator.Creat eInstance(t)
End If
Next

End Function

--
Matt Berther
http://www.mattberther.com
Hi there,

I have an interface (called AuthenticationI nterface) shown below:

Public Interface AuthenticationI nterface
Function IsLoggedIn() As Boolean
Function Authenticate(By Val User As LinkDirectoryUs er) As Boolean
Function GetCurrentUser( ) As LinkDirectoryUs er
Function IsUserSuspended (ByVal User As LinkDirectoryUs er) As
Boolean
Function GetAllRoles() As String()
Sub Register(ByVal User As LinkDirectoryUs er)
Sub Logout()
Sub DeleteUser(ByVa l User As LinkDirectoryUs er)
Sub SuspendUser(ByV al User As LinkDirectoryUs er)
Sub UpdateUser(ByVa l User As LinkDirectoryUs er)
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.Cur rent.Server.Map Path("bin\"
& ConfigurationSe ttings.AppSetti ngs("Authentica tionProvider"))
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( "Authentication Interface", True)
If Not objInterface Is Nothing Then
Dim objPlugin As Object =
objDll.CreateIn stance(t.FullNa me)
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 AuthenticationI nterface
(but I can still use the object perfectly), my question is how can I
convert the object loaded to a AuthenticationI nterface 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 , AuthenticationI nterface)

--
Matt Berther
http://www.mattberther.com
Hi there,

I have an interface (called AuthenticationI nterface) shown below:

Public Interface AuthenticationI nterface
Function IsLoggedIn() As Boolean
Function Authenticate(By Val User As LinkDirectoryUs er) As Boolean
Function GetCurrentUser( ) As LinkDirectoryUs er
Function IsUserSuspended (ByVal User As LinkDirectoryUs er) As
Boolean
Function GetAllRoles() As String()
Sub Register(ByVal User As LinkDirectoryUs er)
Sub Logout()
Sub DeleteUser(ByVa l User As LinkDirectoryUs er)
Sub SuspendUser(ByV al User As LinkDirectoryUs er)
Sub UpdateUser(ByVa l User As LinkDirectoryUs er)
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.Cur rent.Server.Map Path("bin\"
& ConfigurationSe ttings.AppSetti ngs("Authentica tionProvider"))
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( "Authentication Interface", True)
If Not objInterface Is Nothing Then
Dim objPlugin As Object =
objDll.CreateIn stance(t.FullNa me)
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 AuthenticationI nterface
(but I can still use the object perfectly), my question is how can I
convert the object loaded to a AuthenticationI nterface 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.InvalidC astException: 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.InvalidC astException: 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.Curre nt()
Dim authInterface As AuthenticationI nterface = CType(myObj, AuthenticationI nterface)

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

In C#, you would do:

AuthenticationI nterface authInterface = (Authentication Interface)someC lass.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
13914
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 something like: char chars = "Hello!"; byte bytes = (byte) chars;
3
1763
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 that it was a somewhat futile exercise since one of the main requirements was for the existing API (a functional interface written in C) to remain the same. I would have much rathered that the mandate be ab-initio, but that was not the case...
3
2398
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 object, see sample beloz public DoSomething(object theClass) { }
8
4557
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 XML, and embedded the array as some child element in an xml file, i.e.: <Mask>bHQAAH4AAABCTX4AAAAAAAAAPgAAACgAAAAQAAAAEAAAAAEAAQAAAAAAQAAAAAAAAAAAAA AA AAAAAAAAAAAAAAAA////AP//AAD//wAA//8AAP//AAD/7wAA//cAALtzAABVeQAAVUAAAFVA...
11
1978
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 advance. Eric
9
2559
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) example: In Project RoObjDefs: RoPerson.cls file: Public Property Get FirstName() as String Public Property Get LastName() as String <end of file RoPerson.cls> RoPersons.cls file Public Function Count() as Integer
7
2493
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 answers to. First, we have a lot of UserControls. I am getting the error "Type uc_mc_btn_footer is not defined." where uc_mc_btn_footer is the name of the UserControl. Also, on almost all of our UserControls the conversion has remmed out all my code...
2
1431
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 (IDataProviderBase)(((ConstructorInfo)cache).Invoke(null) );
5
9599
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 class to produce values in the system. For instance I could create class MsgGenericValue<UInt16>() which would represent an unsigned value on the interface.
0
8246
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8631
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8341
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8490
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6112
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5570
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4184
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2612
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1489
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.