কম্পিউটার টিউটোরিয়াল

C#-এ রিফ্লেকশন (Reflection) কী এবং ব্যবহার


রানটাইমে টাইপ ইনফরমেশন পাওয়ার জন্য রিফ্লেকশন অবজেক্ট ব্যবহার করা হয়। একটি রানিং প্রোগ্রামের মেটাডেটা অ্যাক্সেস দেয় এমন ক্লাসগুলো System.Reflection নেমস্পেসে থাকে।

System.Reflection ক্লাসের MemberInfo অবজেক্টটি ইনিশিয়ালাইজ করতে হবে একটি ক্লাসের সাথে যুক্ত অ্যাট্রিবিউট আবিষ্কার করার জন্য।

নিচের উদাহরণে আমরা টার্গেট ক্লাসের অবজেক্ট সেট করেছি:

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

  1. C#-এ ইউজার-ডিফাইনড এক্সেপশন (User-defined Exceptions) কী?

  2. C#-এ নেমস্পেস (Namespaces) কী?

  3. C#-এ ডাইনামিক অ্যারে (Dynamic Arrays) কী?

  4. C#-এ কনটেক্সচুয়াল কীওয়ার্ড (Contextual Keywords) কী?