Dynamic invocation and interfaces

  • Thread starter Chakravarthy Bollapalli \(IFIN SCC AI\)
  • Start date
C

Chakravarthy Bollapalli \(IFIN SCC AI\)

Hi,
I have a .net dll (say dll assembly) which has a class implimenting an
interface defined in a application (say application assembly)
so I add the reference of the application assembly to the dll project and
compile. I have a dll available.

from the application assembly i create an instance of the class defined in
dll assembly.

When i try to typecast the instance into an interface instance I get an
exception
--
An unhandled exception of type 'System.InvalidCastException' occurred in
test.exe

Additional information: Specified cast is not valid.
--

but I am able to invoke methods on the instance using
Type.InvokeMember(......) method.

what could be wrong?

kalyan
 
D

Dmitriy Lapshin [C# / .NET MVP]

Hi,

I'd declare the interface in the DLL instead. Referencing the application
assembly from a DLL is definitely not a good idea and it should be avoided.

--
Sincerely,
Dmitriy Lapshin [C# / .NET MVP]
Bring the power of unit testing to the VS .NET IDE today!
http://www.x-unity.net/teststudio.aspx

"Chakravarthy Bollapalli (IFIN SCC AI)"
 
C

Chakravarthy Bollapalli \(IFIN SCC AI\)

Hi,
May be its not a good idea to declare it in the application assembly but
my requirement is that I declare it in application assembly.
Infact there is one such example in oreilly's "Programming Visual C#
..NET 2003" chapter 18.

I am not sure what I am missing some compilation option or did visual
studio screw it up.

Anticipating a response

regards
kalyan


Dmitriy Lapshin said:
Hi,

I'd declare the interface in the DLL instead. Referencing the application
assembly from a DLL is definitely not a good idea and it should be avoided.

--
Sincerely,
Dmitriy Lapshin [C# / .NET MVP]
Bring the power of unit testing to the VS .NET IDE today!
http://www.x-unity.net/teststudio.aspx

"Chakravarthy Bollapalli (IFIN SCC AI)"
Hi,
I have a .net dll (say dll assembly) which has a class implimenting an
interface defined in a application (say application assembly)
so I add the reference of the application assembly to the dll project and
compile. I have a dll available.

from the application assembly i create an instance of the class defined in
dll assembly.

When i try to typecast the instance into an interface instance I get an
exception
--
An unhandled exception of type 'System.InvalidCastException' occurred in
test.exe

Additional information: Specified cast is not valid.
--

but I am able to invoke methods on the instance using
Type.InvokeMember(......) method.

what could be wrong?

kalyan
 
R

Ravichandran J.V.

When there is no relationship between types, you are not allowed to
typecast.

Try declaring the interface elsewhere and not in the application dll.

with regards,


J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandran+J.V.&cob=aspnetpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID=P3966388&BN=999&PN=2
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com
 

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

Top