खोज…


संयोजन क्या होता है?

असेंबली किसी भी सामान्य भाषा रनटाइम (CLR) अनुप्रयोग का निर्माण खंड हैं। प्रत्येक प्रकार जिसे आप परिभाषित करते हैं, उसके तरीकों, गुणों और उनके बाइटकोड के साथ, एक विधानसभा के अंदर संकलित और पैक किया जाता है।

using System.Reflection;

Assembly assembly = this.GetType().Assembly;   

असेंबलियाँ स्व-दस्तावेजीकरण हैं: उनमें न केवल प्रकार, विधियाँ और उनके IL कोड होते हैं, बल्कि मेटाडेटा का निरीक्षण करना और उनका उपभोग करना भी आवश्यक है, दोनों संकलन और क्रम में:

Assembly assembly = Assembly.GetExecutingAssembly();

foreach (var type in assembly.GetTypes())
{
    Console.WriteLine(type.FullName);
}

सभाओं में ऐसे नाम होते हैं जो उनकी पूर्ण, विशिष्ट पहचान का वर्णन करते हैं:

Console.WriteLine(typeof(int).Assembly.FullName);
// Will print: "mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"

यदि इस नाम में PublicKeyToken शामिल है, तो इसे एक मजबूत नाम कहा जाता है। असेंबली का नामकरण एक निजी कुंजी का उपयोग करके एक हस्ताक्षर बनाने की प्रक्रिया है जो विधानसभा के साथ वितरित सार्वजनिक कुंजी से मेल खाती है। यह हस्ताक्षर असेंबली मेनिफ़ेस्ट में जोड़ा जाता है, जिसमें असेंबली बनाने वाली सभी फ़ाइलों के नाम और हैश होते हैं, और इसका PublicKeyToken नाम का हिस्सा बन जाता है। समान नाम वाले विधानसभाओं को समान होना चाहिए; मजबूत नामों का उपयोग संस्करण बनाने और विधानसभा संघर्षों को रोकने के लिए किया जाता है।

परावर्तन का उपयोग करके T की वस्तु कैसे बनाएं

डिफ़ॉल्ट कंस्ट्रक्टर का उपयोग करना

T variable = Activator.CreateInstance(typeof(T));

पैरामीटराइज्ड कंस्ट्रक्टर का उपयोग करना

T variable = Activator.CreateInstance(typeof(T), arg1, arg2);

प्रतिबिंब का उपयोग करके ऑब्जेक्ट बनाना और गुण सेट करना

आइए हम कहते हैं कि हमारे पास एक वर्ग Classy है जिसमें संपत्ति प्रॉपरटुआ है

public class Classy
{
    public string Propertua {get; set;}
}

प्रतिबिंब का उपयोग करके Propertua सेट करने के लिए:

var typeOfClassy = typeof (Classy);
var classy = new Classy();
var prop = typeOfClassy.GetProperty("Propertua");
prop.SetValue(classy, "Value");

प्रतिबिंब के साथ एक एनम का गुण प्राप्त करना (और इसे कैशिंग करना)

गुणसूत्रों पर मेटाडेटा को चिह्नित करने के लिए उपयोगी हो सकते हैं। इसका मूल्य प्राप्त करना धीमा हो सकता है, इसलिए परिणामों को कैश करना महत्वपूर्ण है।

    private static Dictionary<object, object> attributeCache = new Dictionary<object, object>();

    public static T GetAttribute<T, V>(this V value)
        where T : Attribute
        where V : struct
    {
        object temp;

        // Try to get the value from the static cache.
        if (attributeCache.TryGetValue(value, out temp))
        {
            return (T) temp;
        }
        else
        {
            // Get the type of the struct passed in.
            Type type = value.GetType();   
            FieldInfo fieldInfo = type.GetField(value.ToString());

            // Get the custom attributes of the type desired found on the struct.
            T[] attribs = (T[])fieldInfo.GetCustomAttributes(typeof(T), false);

            // Return the first if there was a match.
            var result = attribs.Length > 0 ? attribs[0] : null;

            // Cache the result so future checks won't need reflection.
            attributeCache.Add(value, result);

            return result;
        }
    }

प्रतिबिंब के साथ दो वस्तुओं की तुलना करें

public class Equatable
{
    public string field1;

    public override bool Equals(object obj)
    {
        if (ReferenceEquals(null, obj)) return false;
        if (ReferenceEquals(this, obj)) return true;

        var type = obj.GetType();
        if (GetType() != type)
            return false;

        var fields = type.GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
        foreach (var field in fields)
            if (field.GetValue(this) != field.GetValue(obj))
                return false;

        return true;
    }

    public override int GetHashCode()
    {
        var accumulator = 0;
        var fields = GetType().GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
        foreach (var field in fields)
            accumulator = unchecked ((accumulator * 937) ^ field.GetValue(this).GetHashCode());

        return accumulator;
    }
}

नोट: यह उदाहरण सादगी के लिए एक क्षेत्र आधारित तुलना (स्थिर क्षेत्रों और गुणों को अनदेखा) करता है



Modified text is an extract of the original Stack Overflow Documentation
के तहत लाइसेंस प्राप्त है CC BY-SA 3.0
से संबद्ध नहीं है Stack Overflow