模板参数的 C# void 等效项

发布于 2025-01-06 03:37:40 字数 537 浏览 0 评论 0 原文

我的问题的背景是我正在使用 MonoDroid 编写一个程序来使用 WCF 服务。我想要一些与 C# 中的 SOAP 相关的 C# 库,因此 MonoDroid 是一个明显的选择。

我试图将 void 作为模板参数传递给 AsyncTask。

Xamarin(MonoDroid 的开发者)在 AsyncTask 上提供的文档还有很多不足之处,可以在这里找到:http://docs.mono-android.net/?link=C%3aAndroid.OS.AsyncTask

我需要做的,是用 Java 定义的 AsyncTask 完成的像这样

        public class SoapRequestTask extends AsyncTask<Void, Void, String>

void 模板参数的 C# 等效项是什么?

The context of my question is that I'm using MonoDroid to write a program to consume WCF services. I wanted some of the C# libraries regarding SOAP in C# so MonoDroid was an obvious choice.

I am stuck trying to pass void as template parameter to AsyncTask.

The documentation that Xamarin (developers of MonoDroid) gives on AsyncTask leaves much to be desired and can be found here : http://docs.mono-android.net/?link=C%3aAndroid.OS.AsyncTask

What I need to do, I accomplished in Java with an AsyncTask defined like this

        public class SoapRequestTask extends AsyncTask<Void, Void, String>

What is the C# equivalent of a void template parameter?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

殊姿 2025-01-13 03:37:40

我实际上为任何感兴趣的人找到了答案:

语法是

    private class SoapTask : AsyncTask{}

它的工作方式就像java中的AsyncTask,但参数都是Java.Lang.Objects(除了参数类型是Java.Lang.Objects [] ),因此您可以根据需要在正文中设置它们。

I actually found the answer to this for anyone who is interested:

The syntax is

    private class SoapTask : AsyncTask{}

And it works just like AsyncTask in java, but the parameters are all Java.Lang.Objects (with the exception that the parameters type is a Java.Lang.Objects[] ) so you can just set them as needed in the body.

风吹过旳痕迹 2025-01-13 03:37:40

对我来说,看起来您实际上根本没有使用 void 类型,它只是在示例中用于表示“任何类型都可以放在这里”。阅读 AsyncTask 的“通用类型段落”意味着这 3 种类型分别代表参数类型、进度类型和结果类型。例如,如果您创建了一个声明为的类:

public class MyStringTask extends AsyncTask<String, Integer, Long> {
...
}

作为参数传递给执行方法的对象类型将是字符串

var myStringTask = new MyStringTask();
myStringTask.execute("the first string", "the second string");

进度将以整数值形式通知,结果值将以长整型形式提供。

在 C# 中,您通常会看到它声明为:

public abstract class AsyncTask<TParam, TProgress, TResult>

使用“T”语法作为类型参数并实现(使用上面的示例)为:

public class MyStringTask : AsyncTask<string, int, long>
{
    ...
}

To me, it looks like you don't actually use the void type at all, it's simply used in the example to mean "any type can go here". Reading the AsyncTask's 'generic types paragraph' implies that the 3 types are to represent the parameter type, progress type and result type. so for example, if you created a class declared as:

public class MyStringTask extends AsyncTask<String, Integer, Long> {
...
}

The type of object you pass as the parameter to the execute method would be a string

var myStringTask = new MyStringTask();
myStringTask.execute("the first string", "the second string");

The progress would be notified as an Integer value and the result value would be supplied as a Long.

In C#, you would usually see this declared as:

public abstract class AsyncTask<TParam, TProgress, TResult>

using the 'T' syntax for the type parameters and implemented (using the above example) as:

public class MyStringTask : AsyncTask<string, int, long>
{
    ...
}
旧街凉风 2025-01-13 03:37:40

在 C# 中,您不能使用 void (又名 System.Void) 作为泛型类型参数。 .Net 库通过使用多个“重载”类型来解决这个问题,例如 TaskTask

但我不了解 MonoDroid,所以可能有一些特定于它的解决方案。文档似乎表明有一种特殊类型 Java.Lang.Void 可以使用(而不是这里不能使用的普通 .Net void) 。

In C#, you just can't use void (a.k.a. System.Void) as a generic type parameter. The .Net libraries solve this by having multiple "overloaded" types, like Task and Task<T>.

But I don't know MonoDroid, so there might be some solution specific to it. The documentation seems to suggest there is a special type Java.Lang.Void, that could be used (instead of the normal .Net void that can't be used here).

自演自醉 2025-01-13 03:37:40

C# 中没有与 Void 等效的标准。该类型确实存在,但不能用作泛型类型参数。

最接近的示例是 F# 使用 Unit 作为值不感兴趣的槽的占位符。在 C# 中很容易复制它

public sealed class Unit { 
  public static Unit Value { get { return null; } }
  public static implicit operator Unit(object source) {
    return Value;
  }
}

然后可以用于您不关心的值

public class SoapRequestTask : AsyncTask<Unit, Unit, String> { 
  ...
}

There is no standard equivalent of Void in C#. The type does exist but it's not available as a generic type argument.

The closest example is F#'s use of Unit as a place holder for slots for which the value is uninteresting. It's easy to replicate in C#

public sealed class Unit { 
  public static Unit Value { get { return null; } }
  public static implicit operator Unit(object source) {
    return Value;
  }
}

It can then be used for values you don't care about

public class SoapRequestTask : AsyncTask<Unit, Unit, String> { 
  ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文