2022-06-15【c#】反射

2022-06-15  本文已影响0人  持刀的要迟到了

C#中反射获取某类的子类和根据类型名动态创建对象 - 百度文库 (baidu.com)

C#基础 方法之IsAssignableFrom_Ca_va的博客-CSDN博客_c# isassignablefrom

        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();
                }
            }
上一篇下一篇

猜你喜欢

热点阅读