简体   繁体   中英

Calling a C# library function with out parameters in javascript

If I have a C# function with an out parameter, I cannot get the out parameter when called from javascript. Is this possible?

For example...

public class Test
{
   public bool function doSomething(string txt, out string retTxt)
   {
      ...
      retTxt = "some return value";
      return true;
   }
}

Javascript code..

   var outStr;
   var ret = testInstance.doSomething("instr", outStr);

The official msdn documentation defines out parameter modifier as "causing arguments to be passed by reference". Passing by reference would mean that you are passing the memory location of the variable, which would be impossible in a client/server situation.

Out parameters used in this fashion is not a good idea. If you call a method you would expect it to do its job, if it can't it should throw, however if you really need that bool then you can wrap your output into a class and return that. In this case you can use an anonymous type if your using dot net 4.0 or later.

public class Test
{
   public object doSomething(string txt)
   {
      ...
      return new 
      {
          text = "some return value",
          success = true
      };
   }
}


var result = testInstance.doSomething("instr");
//    result.text
//    result.success

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM