views:

300

answers:

4

Can you call a vb.net api function using reflection from javascript code?

I just started playing around with reflection, I have this snippet of code that works, I want to change it to a javascript page.

Dim RawPlugin As Reflection.Assembly
RawPlugin = Reflection.Assembly.LoadFrom("C:\Inetpub\wwwroot\demo\MasterApplication\getSession\bin\Debug\getSession.dll")

Dim Instance As Object
Instance = RawPlugin.CreateInstance("getSession.class1", True, _
   Reflection.BindingFlags.Default, Nothing, Nothing, Nothing, Nothing)

theValue = Instance.getSessionValue(Session).ToString

Does anyone know if this is possible?

A: 

NO, you can't use reflections, or anything .Net for that matter, directly from javascript.

Greg Dean
LOL - OK I'm wrong. Have a good time reflecting in javascript
Greg Dean
Did you look at your question? I assure you that the answer to the question "Can you call a vb.net api function from javascript using reflection?" is undoubtedly NO. Use AJAX all you want it does not "change [your code] to javascript".
Greg Dean
Relax guys, don't get so upset. He is using an Ajax call to trigger a function on the server which happens to be using reflection to get it's job done... He's not doing reflection directly from Javascript.
Roberto Sebestyen
I'm not the least bit upset, more like amused. He may have chosen AJAX as a solution, but his question clearly states he wants to "call a vb.net function using reflection from javascript code."
Greg Dean
but thats exactly what my solution does.
Greg R
+2  A: 

Client side code doesn't speak directly to server side code. If the information you are looking for is unaffected in between page requests by the user, then you have two options: output the server side value to the client with the page request (so it's value is inside a JavaScript variable on the page), or make it an ajax call. If the information could possibly be stale in between page requests, then you're only option is to return the value from an ajax call.

Rich
awesome, thats exactly what we needed to fix the issue... some times its necessary to think outside the boxes..
Greg R
+1  A: 

In ASP.Net, the .Net code runs on your web server. Javascript runs on the user's computer, in their browser. That user might not even have Windows, let alone the .Net runtime.

For that matter, your user might not even have javascript enabled.

Joel Coehoorn
What if for his program is an in-house application used by employees, and the requirement to use it is to have javascript enabled? Then the issue of where there javascript is enabled disappears.
Roberto Sebestyen
also note that he is trying to trigger that piece of code on the server via Ajax call. not directly calling reflection in javascript like you might think.
Roberto Sebestyen
thats correct, i should have specified in my post that those were the requirements, that everyone has javascript enabled.
Greg R
A: 

the soution:

you can create callbacks using ajax, to call a aspx page, that on page load, runs the code behind, that then creates the reflection in .net, and then passes the end result back to the javascript side, this works, i know this does... however this means that inside the project you need to have the callback page (needs to be compiled if there is any changes).

i guess what i need to do is [use JavaScript ajax to call a callback page that performs the reflection]

Greg R
that's exactly what I said, but for some reason I was downvoted. You can use a JS-only solution like jQuery to call any page in your application and use the resulting text as "web service" data. In this case "web service" is just a generic term for any web resource.
Rich
i upvoted you, as your answer was correct, i dont know who downvoted you..
Greg R