다음을 통해 공유


컴파일러 오류 CS0119

'construct1_name'은 지정된 컨텍스트에서 유효하지 않은 'construct1'입니다.

컴파일러에서 다음과 같은 예기치 않은 구문을 검색했습니다.

  • 클래스 생성자는 조건문의 유효한 테스트 식이 아닙니다.

  • 배열 요소를 참조하기 위해 인스턴스 이름 대신 클래스 이름이 사용되었습니다.

  • 메서드 식별자는 구조체 또는 클래스인 것처럼 사용됩니다.

예시

예제 1

다음 샘플에서는 CS0119를 생성합니다. 'C.B()'는 지정된 컨텍스트에서 유효하지 않은 메서드입니다. 메서드 C.B의 이름을 변경하거나 클래스의 정규화된 이름(예: .)을 B 사용하여 이 오류를 해결할 수 있습니다 N2.B.

namespace N2
{
    public static class B
    {
        public static void X() {}
    }
}

namespace N1
{
    public class C
    {
        void B() {}
        void M() => B.X();   // CS0119
    }
}

예제 2

다음 샘플에서는 클래스 이름을 형식 컬렉션에 추가하려고 할 때 CS0119가 발생하는 일반적인 시나리오를 보여 줍니다. 컴파일러에는 Type 클래스 이름이 아닌 인스턴스가 필요합니다.

using System;
using System.Collections.Generic;

public class Page_BaseClass { }
public class Page_CRTable { }
public class Page_DBGridWithTools { }

public static class PageTypeManager
{
    public static List<Type> GetPageTypes()
    {
        List<Type> pageTypesList = new List<Type>();
        
        // CS0119: 'Page_BaseClass' is a type, which is not valid in the given context
        pageTypesList.Add(Page_BaseClass);
        pageTypesList.Add(Page_CRTable);
        pageTypesList.Add(Page_DBGridWithTools);
        
        return pageTypesList;
    }
}

이 오류를 해결하려면 Type.GetType 메서드 또는 typeof 연산자를 사용하여 Type 인스턴스를 가져옵니다.

using System;
using System.Collections.Generic;

public static class PageTypeManager
{
    public static List<Type> GetPageTypes()
    {
        List<Type> pageTypesList = new List<Type>();
        
        // Use typeof operator to get Type instances
        pageTypesList.Add(typeof(Page_BaseClass));
        pageTypesList.Add(typeof(Page_CRTable));
        pageTypesList.Add(typeof(Page_DBGridWithTools));
        
        return pageTypesList;
    }
    
    // Alternative: Load types dynamically by name
    public static List<Type> GetPageTypesByName(string[] typeNames)
    {
        List<Type> pageTypesList = new List<Type>();
        
        foreach (string typeName in typeNames)
        {
            Type type = Type.GetType(typeName);
            if (type != null)
            {
                pageTypesList.Add(type);
            }
        }
        
        return pageTypesList;
    }
}