Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VB to C# Translation

In VB (ASP.NET)

Application("myapp")= Server.CreateObject("APP.Engine")

aa = Application("myapp").myMethod(2)

works.

In C# I tried

Application["myapp"]= Server.CreateObject("APP.Engine")

but

Application["myapp"].myMethod(2)

fails with

'object' does not contain a definition for 'myMethod'

How can I expose the public interface in C#?

like image 882
AAsk Avatar asked Dec 28 '22 08:12

AAsk


2 Answers

If you have access to the defining type (i.e. not a raw COM object), you can simply cast:

((APP.Engine)Application["myapp"]).myMethod(2);

If you are using c# 4.0, you can do:

dymamic myApp = Application["myapp"];
myApp.myMethod(2);

Otherwise you will have to use dynamic method invocation using reflection and Type.InvokeMember(...)

like image 174
Philip Rieck Avatar answered Jan 08 '23 17:01

Philip Rieck


You need to cast to the correct class first, like:

((APP.Engine)Application["myapp"]).myMethod(2)
like image 40
Bob Avatar answered Jan 08 '23 17:01

Bob