Reflection trong C#

Các đối tượng Reflection được sử dụng để thu được thông tin kiểu tại runtime. Các lớp này cung cấp truy cập tới metadata của một chương trình đang chạy là trong System.Reflection namespace trong C#.

System.Reflection namespace trong C# chứa các lớp mà cho phép bạn thu được thông tin về ứng dụng và để thêm các kiểu, giá trị, và các đối tượng một cách động tới Application.

Các ứng dụng của Reflection

Reflection có các ứng dụng sau:

  • Nó cho phép quan sát thông tin attribute tại runtime.
  • Nó cho phép thẩm tra các kiểu đa dạng trong một Assembly và khởi tạo các kiểu này.
  • Nó cho phép Late Binding tới các phương thức và các thuộc tính.
  • Nó cho phép tạo các kiểu mới tại runtime và sau đó thực hiện một số tác vụ bởi sử dụng những kiểu này.

Quan sát Metadata trong C

Chúng ta đã đề cập trong chương trước rằng với việc sử dụng Reflection, bạn có thể quan sát thông tin attribute.

Đối tượng MemberInfo của lớp System.Reflection trong C# cần được khởi tạo để phát hiện ra các attribute được liên kết với một lớp. Để làm điều này, bạn định nghĩa một đối tượng của lớp target, như:

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

Ví dụ sau minh họa điều này: tạo 3 lớp có tên lần lượt là HelpAttribute, MyClass, TestCsharp như sau:

Lớp HelpAttribute

using System;


namespace HoclaptrinhCsharp
{
    [AttributeUsage(AttributeTargets.All)]
    public class HelpAttribute : System.Attribute
    {
        public readonly string Url;


        public string Topic   // Topic la mot name parameter
        {
            get
            {
                return topic;
            }
            set
            {
                topic = value;
            }
        }


        public HelpAttribute(string url) // url la mot positional parameter
        {
            this.Url = url;
        }
        private string topic;
    }
}

Lớp MyClass

using System;


namespace HoclaptrinhCsharp
{
    [HelpAttribute("Thong tin tren lop MyClass")]
    class MyClass
    {
    }
}

Lớp TestCsharp

using System;


namespace HoclaptrinhCsharp
{
    class TestCsharp
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Reflection trong C#");
            Console.WriteLine("--------------------------");


            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();
        }
    }
}

Nếu bạn không sử dụng lệnh Console.ReadKey(); thì chương trình sẽ chạy và kết thúc luôn (nhanh quá đến nỗi bạn không kịp nhìn kết quả). Lệnh này cho phép chúng ta nhìn kết quả một cách rõ ràng hơn.

Biên dịch và chạy chương trình C# trên sẽ cho kết quả sau: hiển thị tên của các custom attribute được đính kèm tới lớp MyClass

Ví dụ

Trong ví dụ này, chúng ta sử dụng attribute là DeBugInfo được tạo trong chương trước và sử dụng Reflection để đọc metadata trong Rectangle class.

Tạo 3 lớp có tên lần lượt là

Lớp Rectangle:

using System;


namespace HoclaptrinhCsharp
{
    [DeBugInfo(45, "Tran Nam", "2/8/2016", Message = "Kieu tra ve khong hop le")]
    [DeBugInfo(49, "Minh Chinh", "10/10/2016", Message = "Bien khong duoc su dung")]


    class Rectangle
    {
        //cac bien thanh vien
        protected double chieu_dai;
        protected double chieu_rong;
        public Rectangle(double l, double w)
        {
            chieu_dai = l;
            chieu_rong = w;
        }
        [DeBugInfo(55, "Tran Nam", "2/8/2016", Message = "Kieu tra ve khong hop le")]
        public double tinhDienTich()
        {
            return chieu_dai * chieu_rong;
        }
        [DeBugInfo(56, "Minh Chinh", "19/10/2016")]
        public void Display()
        {
            Console.WriteLine("Chieu dai: {0}", chieu_dai);
            Console.WriteLine("Chieu rong: {0}", chieu_rong);
            Console.WriteLine("Dien tich: {0}", tinhDienTich());
        }
    }
}

Lớp DeBugInfo:

using System;
using System.Reflection;


namespace HoclaptrinhCsharp
{
    //Mot custom attribute BugFix 
    [AttributeUsage(AttributeTargets.Class |
    AttributeTargets.Constructor |
    AttributeTargets.Field |
    AttributeTargets.Method |
    AttributeTargets.Property,
    AllowMultiple = true)]


    public class DeBugInfo : System.Attribute
    {
        private int bugNo;
        private string developer;
        private string lastReview;
        public string message;


        public DeBugInfo(int bg, string dev, string d)
        {
            this.bugNo = bg;
            this.developer = dev;
            this.lastReview = d;
        }


        public int BugNo
        {
            get
            {
                return bugNo;
            }
        }


        public string Developer
        {
            get
            {
                return developer;
            }
        }


        public string LastReview
        {
            get
            {
                return lastReview;
            }
        }


        public string Message
        {
            get
            {
                return message;
            }
            set
            {
                message = value;
            }
        }
    }


}

Lớp TestCsharp:

using System;
using System.Reflection;


namespace HoclaptrinhCsharp
{
    class TestCsharp
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Reflection trong C#");
            Console.WriteLine("------------------------------------");


            Rectangle r = new Rectangle(4.5, 7.5);
            r.Display();
            Type type = typeof(Rectangle);


            //lap qua cac attribtue cua lop Rectangle
            foreach (Object attributes in type.GetCustomAttributes(false))
            {
                DeBugInfo dbi = (DeBugInfo)attributes;
                if (null != dbi)
                {
                    Console.WriteLine("Bug no: {0}", dbi.BugNo);
                    Console.WriteLine("Developer: {0}", dbi.Developer);
                    Console.WriteLine("Last Reviewed: {0}", dbi.LastReview);
                    Console.WriteLine("Remarks: {0}", dbi.Message);
                }
            }


            //lap qua cac method attribtue
            foreach (MethodInfo m in type.GetMethods())
            {
                foreach (Attribute a in m.GetCustomAttributes(true))
                {
                    DeBugInfo dbi = (DeBugInfo)a;
                    if (null != dbi)
                    {
                        Console.WriteLine("Bug no: {0}, for Method: {1}", dbi.BugNo, m.Name);
                        Console.WriteLine("Developer: {0}", dbi.Developer);
                        Console.WriteLine("Last Reviewed: {0}", dbi.LastReview);
                        Console.WriteLine("Remarks: {0}", dbi.Message);
                    }
                }
            }


            Console.ReadLine();
            Console.ReadKey();
        }
    }
}

Biên dịch và chạy chương trình C# trên sẽ cho kết quả sau:

Reflection trong C# 1


Bình luận