4

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

Jason Berkan
  • 8,734
  • 7
  • 29
  • 39
AAsk
  • 1,431
  • 4
  • 17
  • 25
  • Hang on, does this mean that VB.NET does a dynamic invocation for every method call on the `System.Object` type? Otherwise, why is no cast necessary in VB.NET here? – Timwi Aug 31 '10 at 19:55
  • @Timwi: Yes, it does mean that, believe it or not. This is why [extension methods defined on the `Object` type don't behave as most developers expect](http://stackoverflow.com/questions/2402660/why-does-this-extension-method-throw-a-nullreferenceexception-in-vb-net). – Dan Tao Aug 31 '10 at 20:39
  • @Timwi only if Option Strict is 'Off', which (unfortunately IMO) is the default – jeroenh Aug 31 '10 at 22:57

5 Answers5

7

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
  • 32,368
  • 11
  • 87
  • 99
  • 3
    Woah... lets not go flinging around dynamic so lightly. – ChaosPandion Aug 31 '10 at 19:33
  • 2
    @ChaosPandion - For COM calls from c#, dynamic fits the bill quite nicely, I think - hardly lightly. – Philip Rieck Aug 31 '10 at 19:37
  • 1
    @ChaosPandion: COM is where `dynamic` is most useful! – Brian Gideon Aug 31 '10 at 19:39
  • @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 Aug 31 '10 at 19:41
  • @Brian - You may not have seen the original answer which did not provide the same info you see now. – ChaosPandion Aug 31 '10 at 19:42
  • @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 Aug 31 '10 at 19:43
  • @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 Sep 08 '10 at 17:48
3

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

((APP.Engine)Application["myapp"]).myMethod(2)
Bob
  • 3,301
  • 1
  • 16
  • 11
0

Just remember one thing while converting code from VB to C# is, C# is the strongly typed language and before you access any type, you will need to convert it. However same wasn't required in VB.

So for this example you would need to use App.Engine for type conversion.

((APP.Engine)Application["myapp"]).myMethod(2)
Amnesh Goel
  • 2,617
  • 3
  • 28
  • 47
0

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
  • 11,257
  • 11
  • 61
  • 97
0

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
  • 2,975
  • 23
  • 38