C# でのリフレクション

WBOY
リリース: 2023-08-26 13:45:02
転載
1272 人が閲覧しました

C# 中的反射

リフレクション オブジェクトは、実行時に型情報を取得するために使用されます。実行中のプログラムのメタデータへのアクセスを許可するクラスは、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 中国語 Web サイトの他の関連記事を参照してください。

ソース:tutorialspoint.com
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!