문제

I have the following method written in 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

I used a code porting tool to convert it to C#. It produced the following method:

public static string formatClassNameAndMethod(string prefix, StackFrame stackFrame)
{
    MethodBase methodBase = StackFrame.GetMethod();

    return prefix + ":[" + stackFrame.GetMethod().DeclaringType.Namespace + "][" + 
            stackFrame.GetMethod().DeclaringType.Name + "." + methodBase.Name + "] ";
}

Unfortunately, Visual Studio now gives me the following error:

Cannot access non-static method 'GetMethod' in static context

It is complaining about StackFrame.GetMethod() because that method is not static. Why is this happening? I understand what the error is, but I don't understand why I didn't get this in VB.NET. Is there a difference between how Shared in VB.NET and static in C# work? Did the conversion tool not properly convert this?

도움이 되었습니까?

해결책 2

VB is case-insensitive - the compiler saw "StackFrame.GetMethod()" and said "Oh, the developer must have meant "stackFrame.GetMethod()".

다른 팁

GetMethod isn't static. This is what it is telling you.

This means you need to create an instance before you can call the method. Your method already has a StackFrame instance passed in.. and this is merely a case of case sensitivity. Lowercase the S.

public static string formatClassNameAndMethod(string prefix, StackFrame stackFrame)
{ //                                                                     ^^^ this
    MethodBase methodBase = stackFrame.GetMethod();
    //                     ^^ lowercase S
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top