>  기사  >  백엔드 개발  >  C#의 리플렉션

C#의 리플렉션

WBOY
WBOY앞으로
2023-08-26 13:45:021247검색

C# 中的反射

Reflection 개체는 런타임에 유형 정보를 얻는 데 사용됩니다. 실행 중인 프로그램의 메타데이터에 대한 액세스를 허용하는 클래스는 System.Reflection 네임스페이스에 있습니다.

다음은 Reflections의 적용입니다. -

  • 런타임에 속성 정보를 볼 수 있게 해줍니다.

  • 어셈블리의 다양한 유형을 검사하고 이러한 유형을 인스턴스화할 수 있습니다.

  • 메서드 및 속성에 대한 후기 바인딩이 가능합니다.
  • 런타임에 새 유형을 생성한 다음 이러한 유형을 사용하여 일부 작업을 수행할 수 있습니다.

  • ul>

    예제를 살펴보겠습니다 -

    Example

    using System;
    
    [AttributeUsage(AttributeTargets.All)]
    public class HelpAttribute : System.Attribute {
       public readonly string Url;
    
       public string Topic // Topic is a named parameter {
          get {
             return topic;
          }
          set {
             topic = value;
          }
       }
       public HelpAttribute(string url) // url is a positional parameter {
          this.Url = url;
       }
       private string topic;
    }
    
    [HelpAttribute("Information on the class MyClass")]
       class MyClass {
    }
    
    namespace AttributeAppl {
       class Program {
          static void Main(string[] args) {
             System.Reflection.MemberInfo info = typeof(MyClass);
             object[] attributes = info.GetCustomAttributes(true);
    
             for (int i = 0; i < attributes.Length; i++) {
                System.Console.WriteLine(attributes[i]);
             }
             Console.ReadKey();
          }
       }
    }

위 내용은 C#의 리플렉션의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
이 기사는 tutorialspoint.com에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제