2022-06-15【c#】反射

C#中反射獲取某類的子類和根據(jù)類型名動態(tài)創(chuàng)建對象 - 百度文庫 (baidu.com)

C#基礎(chǔ) 方法之IsAssignableFrom_Ca_va的博客-CSDN博客_c# isassignablefrom

C# 反射調(diào)用帶參數(shù)的重載的泛型方法_DayDay_Code的博客-CSDN博客

        public virtual void Initialize(object obj, MemberVisibility visibility)
        {
            var properties = Serialization.GetSerializedProperties(obj.GetType(), visibility);
            var valueCount = 0;
            m_Delegates = new BaseDelegate[properties.Length];
            for (int i = 0; i < properties.Length; ++i) {
                // The property may not be valid.
                if (Serialization.GetValidGetMethod(properties[i], visibility) == null) {
                    continue;
                }

                // Create a generic delegate based on the property type.
                var genericDelegateType = typeof(GenericDelegate<>).MakeGenericType(properties[i].PropertyType);
                m_Delegates[valueCount] = Activator.CreateInstance(genericDelegateType) as BaseDelegate;

                // Initialize the delegate.
                if (m_Delegates[valueCount] != null) {
                    m_Delegates[valueCount].Initialize(obj, properties[i], visibility);
                } else {
                    Debug.LogWarning("Warning: Unable to create preset of type " + properties[i].PropertyType);
                }
                valueCount++;
            }
            if (m_Delegates.Length != valueCount) {
                Array.Resize(ref m_Delegates, valueCount);
            }
        }

            public override void Initialize(object obj, PropertyInfo property, MemberVisibility visibility)
            {
                m_SetMethod = property.GetSetMethod(visibility != MemberVisibility.Public);
                if (m_SetMethod != null) {
                    m_Setter = (Action<T>)Delegate.CreateDelegate(typeof(Action<T>), obj, m_SetMethod);
                }
                
                var getMethod = property.GetGetMethod(visibility != MemberVisibility.Public);
                if (getMethod != null) {
                    m_Getter = (Func<T>)Delegate.CreateDelegate(typeof(Func<T>), obj, getMethod);

                    // Create an instance of the value if it is an array or a list. This will allow a snapshot of the array/list elements to be saved without having the
                    // array/list change because it is later modified by reference.
                    var type = typeof(T);
                    m_IsIList = typeof(IList).IsAssignableFrom(type);
                    if (m_IsIList) {
                        if (typeof(T).IsArray) {
                            var value = m_Getter() as Array;
                            m_Value = (T)(object)Array.CreateInstance(type.GetElementType(), value == null ? 0 : value.Length);
                        } else {
                            var baseType = type;
                            while (!baseType.IsGenericType) {
                                baseType = baseType.BaseType;
                            }
                            var elementType = baseType.GetGenericArguments()[0];
                            if (type.IsGenericType) {
                                m_Value = (T)Activator.CreateInstance(typeof(List<>).MakeGenericType(elementType));
                            } else {
                                m_Value = (T)Activator.CreateInstance(type);
                            }
                        }
                    }

                    // The value should be set at the same time the delegate is initailized.
                    UpdateValue();
                }
            }

8.22


性能不如 new t();t是泛型,泛型可以有 無參構(gòu)造 約束 T : new()



8.24
C#反射:PropertyInfo、FieldInfo和MemberInfo的區(qū)別_aSong~的博客-CSDN博客_c# fieldinfo propertyinfo

BindingFlags:

BindingFlags Enum (System.Reflection) | Microsoft Docs
C# BindingFlags類代碼示例 - 純凈天空 (vimsky.com)

  • CreateInstance
    Specifies that reflection should create an instance of the specified type. Calls the constructor that matches the given arguments. The supplied member name is ignored. If the type of lookup is not specified, (Instance | Public) will apply. It is not possible to call a type initializer.
    This flag is passed to an InvokeMember method to invoke a constructor.
    指定了該反射,應(yīng)該針對該類型,創(chuàng)建一個實例。調(diào)用符合給定參數(shù)的構(gòu)造函數(shù)。提供的成員名是被忽略的。如果沒有指定查找類型,將默認應(yīng)用(Instance|Public)。不可能調(diào)用一個類型初始化器。
    這個標志傳遞給一個InvokeMember方法,來調(diào)用構(gòu)造函數(shù)。
    翻譯:會創(chuàng)建一個默認對象。

  • DeclaredOnly
    Specifies that only members declared at the level of the supplied type's hierarchy should be considered. Inherited members are not considered.
    指定了只有聲明在支持類型層級結(jié)構(gòu)的成員才被考慮。繼承的成員不被考慮。
    翻譯:沒看懂。只知道繼承的成員不被考慮。

  • Default
    Specifies that no binding flags are defined.

  • DoNotWrapExceptions

  • ExactBinding
    Specifies that types of the supplied arguments must exactly match the types of the corresponding formal parameters. Reflection throws an exception if the caller supplies a non-null Binder object, since that implies that the caller is supplying BindToXXX implementations that will pick the appropriate method. The default binder ignores this flag, while custom binders can implement the semantics of this flag.
    指定了支持的參數(shù)必須和相應(yīng)形參的類型完全匹配。如果調(diào)用者提供非空的Binder對象,反射將拋出異常,因為這意味著調(diào)用者正提供BindToXXX實現(xiàn),這會選擇合適的方法。默認的binder忽略這個標簽,而自定義的binder可以實現(xiàn)這個標志的語義。
    翻譯:沒看懂

  • FlattenHierarchy
    Specifies that public and protected static members up the hierarchy should be returned. Private static members in inherited classes are not returned. Static members include fields, methods, events, and properties. Nested types are not returned.
    指定應(yīng)該返回層次結(jié)構(gòu)上層的公共和受保護的靜態(tài)成員。不返回繼承類中的私有靜態(tài)成員。靜態(tài)成員包括字段、方法、事件和屬性。不返回嵌套類型。
    翻譯:返回非私有靜態(tài)成員。

  • GetField
    Specifies that the value of the specified field should be returned.
    This flag is passed to an InvokeMember method to get a field value.

  • GetProperty
    Specifies that the value of the specified property should be returned.
    This flag is passed to an InvokeMember method to invoke a property getter.

  • IgnoreCase
    Specifies that the case of the member name should not be considered when binding.
    翻譯:忽略大小寫

  • IgnoreReturn
    Used in COM interop to specify that the return value of the member can be ignored.

  • Instance
    Specifies that instance members are to be included in the search.

  • InvokeMethod
    Specifies that a method is to be invoked. This must not be a constructor or a type initializer.
    This flag is passed to an InvokeMember method to invoke a method.
    ...

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容