简体   繁体   English

在javascript中调用不带参数的C#库函数

[英]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. 如果我有一个带有out参数的C#函数,则从javascript调用时无法获取out参数。 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.. Javascript代码

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

The official msdn documentation defines out parameter modifier as "causing arguments to be passed by reference". msdn官方文档将out参数修饰符定义为“导致参数通过引用传递”。 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. 以这种方式使用out参数不是一个好主意。 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. 如果您调用一个方法,则希望它能够完成其工作,如果不能,则应抛出该方法,但是,如果您确实需要该bool则可以将输出包装到一个类中并返回它。 In this case you can use an anonymous type if your using dot net 4.0 or later. 在这种情况下,如果使用dot net 4.0或更高版本,则可以使用匿名类型。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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