views:

125

answers:

6

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#?

+3  A: 

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

((APP.Engine)Application["myapp"]).myMethod(2)
Bob
+6  A: 

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(...)

Philip Rieck
Woah... lets not go flinging around dynamic so lightly.
ChaosPandion
@ChaosPandion - For COM calls from c#, dynamic fits the bill quite nicely, I think - hardly lightly.
Philip Rieck
@ChaosPandion: COM is where `dynamic` is most useful!
Brian Gideon
@Phillip - I agree, but we don't know if the object is a COM object. I think your current answer does provide enough information for the OP to make a well informed choice though.
ChaosPandion
@Brian - You may not have seen the original answer which did not provide the same info you see now.
ChaosPandion
@ChaosPandion - yes, my hitting of "shift enter" with current greasemonkey scripts did post a poor partial draft. I'll give you that raw "use dynamic" is not good advice at all.
Philip Rieck
A: 

Cast the result of Application["myapp"] to the correct type, like so:

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

Other options include creating a local reference and using that instead.

Mark
A: 

This basically happens as C# is strongly typed language. When you call Application you are actually calling a collection of type Object. The collection is actually an array of Object. We take it as object because it will enable you to store almost anything to the collection.

Now Application["myapp"] will eventually return an object of System.Object type.

So you need to typecast the object to your type.

App.Engine obj = Application["myapp"] as App.Engine;
if(obj !=  null)
  obj.myMethod(2);

The object obj is a reference to the actual type and the compiler will not throw any exception if you call myMethod(2).

abhishek
A: 

Thank you very much. I haven't resolved my issue yet; I need to study the responses in greater detail. I have some other questions:

With

<%@ WebService Language="C#" Class="WebSAPLCS" %>

How can I specify that C# 4.0 is used?

How can I add a reference dynamically? I.E in code edited in NOTEPAD, say. I can't fathom what Visual Studio does when I add a reference using that IDE.

AAsk
Best to either update the question or else post a new question for this one. The format of this website isn't really conducive to responding to a question posed as an answer in this way.
Dan Tao
A: 

@Philip Rieck: Thanks for the solution

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

Once I corrected the typo & figured how to target framework 4, it worked & works well

AAsk