【发布时间】:2013-10-09 21:53:17
【问题描述】:
我有以下用VB.NET编写的方法:
Public Shared Function formatClassNameAndMethod(ByVal prefix As String, ByVal stackFrame As StackFrame) As String
Dim methodBase As MethodBase = StackFrame.GetMethod()
Return prefix + ":[" + stackFrame.GetMethod().DeclaringType.Namespace + "][" + stackFrame.GetMethod().DeclaringType.Name + "." + methodBase.Name + "] "
End Function
我使用代码移植工具将其转换为 C#。它产生了以下方法:
public static string formatClassNameAndMethod(string prefix, StackFrame stackFrame)
{
MethodBase methodBase = StackFrame.GetMethod();
return prefix + ":[" + stackFrame.GetMethod().DeclaringType.Namespace + "][" +
stackFrame.GetMethod().DeclaringType.Name + "." + methodBase.Name + "] ";
}
不幸的是,Visual Studio 现在给我以下错误:
无法在静态上下文中访问非静态方法“GetMethod”
它抱怨StackFrame.GetMethod(),因为该方法不是静态的。为什么会这样?我明白错误是什么,但我不明白为什么我没有在 VB.NET 中得到这个。 VB.NET 中的 Shared 和 C# 中的 static 的工作方式有区别吗?转换工具没有正确转换这个吗?
【问题讨论】:
标签: c# vb.net static porting shared