美文网首页
2022-06-15【c#】反射

2022-06-15【c#】反射

作者: 持刀的要迟到了 | 来源:发表于2022-06-15 11:30 被阅读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();
                }
            }

相关文章

  • 2022-06-15【c#】反射

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

  • C#它山之石

    C# 使用反射技术实例化指定的类C#之玩转反射Reactive Extensions入门IoC solutions...

  • Unity 之如何写出强壮的代码

    【反射】 Unity C#基础之 反射反射,程序员的快乐 Unity C#基础之 特性,一个灵活的小工具 【多线程...

  • 目录 - C#

    总目录 C# 第01局:泛型 C# 第02局:反射 C# 第03局:特性 C# 第04局:委托 C# 第05局:事...

  • C#特性(Attribute)-现学现用

    前言 想要灵性的使用C#反射机制,特性(Attribute)的使用是必不可少的。 C# 特性(Attribute)...

  • C#反射

    C# 反射(Reflection) 反射指程序可以访问、检测和修改它本身状态或行为的一种能力。程序集包含模块,而模...

  • C#反射

    什么是元数据,什么是反射 程序是用来处理数据的,文本和特性都是数据,而程序本身(类的本身和BLC中的类) 这些也是...

  • C#反射

    根据moduleName反射到某个类某个方法 转换成DataView操作Datatable ![B(IRRLH7`...

  • C#反射

    注意:C#本身已经时候用了反射!!!! 以下了解即可不过,反射可以拿到类中private的字段,这个对编程则有用...

  • Swift中的反射Mirror

    Swift中的反射Mirror [TOC] 前言 Mirror是Swift中的反射机制,对于C#和Java开发人员...

网友评论

      本文标题:2022-06-15【c#】反射

      本文链接:https://www.haomeiwen.com/subject/cvcxvrtx.html