C#
 Computer >> コンピューター >  >> プログラミング >> C#

C#でのリフレクションのアプリケーションは何ですか?


リフレクションオブジェクトは、実行時にタイプ情報を取得するために使用されます。実行中のプログラムのメタデータへのアクセスを提供するクラスは、System.Reflection名前空間にあります。

以下は反射のアプリケーションです-

  • 実行時に属性情報を表示できます。
  • アセンブリ内のさまざまなタイプを調べて、これらのタイプをインスタンス化できます。
  • メソッドとプロパティへの遅延バインディングが可能になります
  • 実行時に新しいタイプを作成し、それらのタイプを使用していくつかのタスクを実行できます。

System.Reflection名前空間には、アプリケーションに関する情報を取得し、タイプ、値、およびオブジェクトをアプリケーションに動的に追加できるクラスが含まれています。

例も見てみましょう。

以下に、ターゲットクラスのオブジェクトを設定しました-

System.Reflection.MemberInfo info = typeof(MyClass);

これが完全な例です-

using System;
using System.Reflection;

[AttributeUsage(AttributeTargets.All)]
public class HelpAttribute : System.Attribute {
   public readonly string Url;
   public string Topic {
      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();
      }
   }
}

出力

HelpAttribute

  1. Cのトークンは何ですか?

    トークンは、コンパイラにとって意味のあるプログラムの最小要素に他なりません。プログラムを最小単位に分割するコンパイラはトークンと呼ばれ、これらのトークンはコンパイルのさまざまな段階に進みます。 タイプ トークンはさまざまなタイプに分類されます。以下に説明します- キーワード 識別子 定数 文字列 特別な記号 オペレーター 例 以下に示すのは、Cプログラムの識別子、キーワード、変数などの使用です。 。 #include <stdio.h> int main(){    int a,b,c;    printf("ente

  2. C#のカスタム例外とは何ですか?

    他のプログラミング言語と同様に、C#では、ユーザー定義の例外を簡単に作成できます。ユーザー定義の例外クラスは、Exceptionクラスから派生します。カスタム例外は、ユーザー定義の例外と呼ばれるものです。 以下の例では、作成された例外は組み込みの例外ではありません。これはカスタム例外です- TempIsZeroException 次のコードを実行して、C#でユーザー定義の例外を作成する方法を学ぶことができます。 例 using System; namespace Demo {    class TestTemperature {