나는 많은 스레드가 이것에 대해 이야기하지만, 지금까지는 내 상황을 직접적으로 돕는 것을 찾지 못했다는 것을 알고 있습니다. 정적 클래스와 비 정적 클래스 모두에서 액세스해야하는 클래스 멤버가 있습니다. 그러나 멤버가 정적이 아닌 경우 정적 메서드에서 멤버를 얻을 수 없습니다.정적 메서드에서 클래스 멤버에 액세스
public class SomeCoolClass
{
public string Summary = "I'm telling you";
public void DoSomeMethod()
{
string myInterval = Summary + " this is what happened!";
}
public static void DoSomeOtherMethod()
{
string myInterval = Summary + " it didn't happen!";
}
}
public class MyMainClass
{
SomeCoolClass myCool = new SomeCoolClass();
myCool.DoSomeMethod();
SomeCoolClass.DoSomeOtherMethod();
}
두 가지 방법 중 하나에서 요약을 가져 오는 것이 좋습니다.
정적 멤버는 * Type *에 속합니다. 비 정적 멤버는 해당 유형의 * 인스턴스에 속합니다. – asawyer
'Summary'가 일정해야합니까? 당신은'public const string Summary'를 표시 할 수 있으며, 둘 다에서 액세스 할 수 있습니다. –