2022-06-15【c#】反射

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

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

C# 反射调用带参数的重载的泛型方法_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)Delegate.CreateDelegate(typeof(Action), obj, m_SetMethod);
                }
                
                var getMethod = property.GetGetMethod(visibility != MemberVisibility.Public);
                if (getMethod != null) {
                    m_Getter = (Func)Delegate.CreateDelegate(typeof(Func), 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是泛型,泛型可以有 无参构造 约束 T : new()



8.24
C#反射:PropertyInfo、FieldInfo和MemberInfo的区别_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.
    指定了该反射,应该针对该类型,创建一个实例。调用符合给定参数的构造函数。提供的成员名是被忽略的。如果没有指定查找类型,将默认应用(Instance|Public)。不可能调用一个类型初始化器。
    这个标志传递给一个InvokeMember方法,来调用构造函数。
    翻译:会创建一个默认对象。

  • DeclaredOnly
    Specifies that only members declared at the level of the supplied type's hierarchy should be considered. Inherited members are not considered.
    指定了只有声明在支持类型层级结构的成员才被考虑。继承的成员不被考虑。
    翻译:没看懂。只知道继承的成员不被考虑。

  • 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.
    指定了支持的参数必须和相应形参的类型完全匹配。如果调用者提供非空的Binder对象,反射将抛出异常,因为这意味着调用者正提供BindToXXX实现,这会选择合适的方法。默认的binder忽略这个标签,而自定义的binder可以实现这个标志的语义。
    翻译:没看懂

  • 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.
    指定应该返回层次结构上层的公共和受保护的静态成员。不返回继承类中的私有静态成员。静态成员包括字段、方法、事件和属性。不返回嵌套类型。
    翻译:返回非私有静态成员。

  • 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.
    ...

你可能感兴趣的:(2022-06-15【c#】反射)