关键字 this 在 C# 的当前上下文中不可用
keyword this is not available in current context in C#
我有一个名为 EventListener 的接口,还有一个名为 Endpoint 的 class,它将 eventlistener 作为方法 newInstance 中的参数 newInstance(boolean debug, EventListener eventListener)
现在在我的 MainActivity class 中,当我实现 EventListener 并创建一个端点实例时,它在 C# 中给出名为 "keyword this is not available in current context" 的错误。
public class MainActivity : Activity, IEventListener {
Endpoint endpoint = Endpoint.newInstance(true, this);
}
令人惊讶的是 java 中没有这个错误。谁能解释一下有什么区别。
初始化字段时不能使用this
。如果要使用 this
.
,则需要在构造函数中初始化该字段
根据 Servy 的回答,您不能在初始化程序 (MDSN) 中使用 this
,但可以从构造函数中使用。这应该大约是您需要的:
public class MainActivity : Activity, IEventListener
{
private Endpoint endpoint; // could possibly be readonly
public MainActivity()
{
endpoint = Endpoint.newInstance(true, this);
}
}
我有一个名为 EventListener 的接口,还有一个名为 Endpoint 的 class,它将 eventlistener 作为方法 newInstance 中的参数 newInstance(boolean debug, EventListener eventListener)
现在在我的 MainActivity class 中,当我实现 EventListener 并创建一个端点实例时,它在 C# 中给出名为 "keyword this is not available in current context" 的错误。
public class MainActivity : Activity, IEventListener {
Endpoint endpoint = Endpoint.newInstance(true, this);
}
令人惊讶的是 java 中没有这个错误。谁能解释一下有什么区别。
初始化字段时不能使用this
。如果要使用 this
.
根据 Servy 的回答,您不能在初始化程序 (MDSN) 中使用 this
,但可以从构造函数中使用。这应该大约是您需要的:
public class MainActivity : Activity, IEventListener
{
private Endpoint endpoint; // could possibly be readonly
public MainActivity()
{
endpoint = Endpoint.newInstance(true, this);
}
}