Пример #1
0
        internal XTypeInfo(Type type, XBindingFlags flags)
        {
            RWObjectAttribute[] rwAttributes = null;

            if ((flags & RW) != 0)
            {
                rwAttributes = type.GetDefinedAttributes <RWObjectAttribute>(true);
            }

            if (rwAttributes != null && rwAttributes.Length != 0)
            {
                foreach (var item in rwAttributes)
                {
                    Switch(ref flags, XBindingFlags.RWIgnoreCase, item.IgnoreCace);

                    Switch(ref flags, XBindingFlags.RWNotFoundException, item.NotFoundException);

                    Switch(ref flags, XBindingFlags.RWCannotGetException, item.CannotGetException);

                    Switch(ref flags, XBindingFlags.RWCannotSetException, item.CannotSetException);

                    Switch(ref flags, XBindingFlags.Property, item.IncludeProperties);

                    Switch(ref flags, XBindingFlags.Field, item.IncludeFields);

                    Switch(ref flags, XBindingFlags.RWSkipDefaultValue, item.SkipDefaultValue);

                    Switch(ref flags, XBindingFlags.RWMembersOptIn, item.MembersOptIn);
                }
            }

            this.type  = type;
            this.flags = flags;

            var fields     = new List <XFieldInfo>();
            var properties = new List <XPropertyInfo>();
            var events     = new List <XEventInfo>();
            var indexers   = new List <XIndexerInfo>();
            var methods    = new List <XMethodInfo>();
            var rwFields   = new List <IXFieldRW>();

            GetItems(type, flags, fields, properties, events, indexers, methods);

            foreach (var item in fields)
            {
                if (item is IXFieldRW rwField && rwField.AfterType.CanBeGenericParameter())
                {
                    var attributes = item.FieldInfo
                                     .GetCustomAttributes(typeof(RWFieldAttribute), true)
                                     ?.OfType <RWFieldAttribute>()
                                     ?.ToList();

                    if (rwAttributes != null && rwAttributes.Length != 0)
                    {
                        foreach (var objectAttribute in rwAttributes)
                        {
                            objectAttribute.OnLoadMember(type, item.FieldInfo, ref attributes);
                        }
                    }

                    if (attributes != null && attributes.Count != 0)
                    {
                        foreach (var attribute in attributes)
                        {
                            XAttributedFieldRW attributedFieldRW = XAttributedFieldRW.Create(rwField, attribute, flags);

                            if (attributedFieldRW.CanRead || attributedFieldRW.CanWrite)
                            {
                                rwFields.Add(attributedFieldRW);
                            }
                        }
                    }
                    else
                    {
                        rwFields.Add(rwField);
                    }
                }
            }

            foreach (var item in properties)
            {
                if (item is IXFieldRW rwField && rwField.AfterType.CanBeGenericParameter())
                {
                    var attributes = item.PropertyInfo
                                     .GetCustomAttributes(typeof(RWFieldAttribute), true)
                                     ?.OfType <RWFieldAttribute>()
                                     ?.ToList();

                    if (rwAttributes != null && rwAttributes.Length != 0)
                    {
                        foreach (var objectAttribute in rwAttributes)
                        {
                            objectAttribute.OnLoadMember(type, item.PropertyInfo, ref attributes);
                        }
                    }

                    if (attributes != null && attributes.Count != 0)
                    {
                        // Attributed property allow access non-Public accessor.
                        item.Initialize(item.propertyInfo, item.flags | XBindingFlags.NonPublic);

                        foreach (RWFieldAttribute attribute in attributes)
                        {
                            XAttributedFieldRW attributedFieldRW = XAttributedFieldRW.Create(rwField, attribute, flags);

                            if (attributedFieldRW.CanRead || attributedFieldRW.CanWrite)
                            {
                                rwFields.Add(attributedFieldRW);
                            }
                        }
                    }
                    else
                    {
                        rwFields.Add(rwField);
                    }
                }
            }

            rwFields.Sort((x, y) => x.Order.CompareTo(y.Order));

            if (rwAttributes != null && rwAttributes.Length != 0)
            {
                var temp = rwFields.Cast <IObjectField>().ToList();

                foreach (var item in rwAttributes)
                {
                    item.OnCreate(type, ref temp);
                }

                rwFields = temp.Cast <IXFieldRW>().ToList();
            }

            this.fields     = new Cache <string, XFieldInfo>();
            this.properties = new Cache <string, XPropertyInfo>();
            this.events     = new Cache <string, XEventInfo>();
            this.indexers   = new Cache <RuntimeParamsSign, XIndexerInfo>();
            this.methods    = new Cache <RuntimeMethodSign, XMethodInfo>();
            this.rwFields   = new Cache <string, IXFieldRW>((flags & XBindingFlags.RWIgnoreCase) != 0 ? StringComparer.OrdinalIgnoreCase : null);

            foreach (var item in fields)
            {
                this.fields.Add(item.Name, item);
            }
            foreach (var item in properties)
            {
                this.properties.Add(item.Name, item);
            }
            foreach (var item in events)
            {
                this.events.Add(item.Name, item);
            }
            foreach (var item in indexers)
            {
                this.indexers.Add(ParametersToTypes(item.PropertyInfo.GetIndexParameters()), item);
            }
            foreach (var item in methods)
            {
                this.methods.Add((item.MethodInfo.Name, ParametersToTypes(item.MethodInfo.GetParameters())), item);
            }
            foreach (var item in rwFields)
            {
                this.rwFields.Add(item.Name, item);
            }

            GC.Collect();
        }
Пример #2
0
        internal XTypeInfo(Type type, XBindingFlags flags)
        {
            RWObjectAttribute[] rwAttributes = null;

            if ((flags & RW) != 0)
            {
                rwAttributes = type.GetDefinedAttributes <RWObjectAttribute>(true);
            }

            if (rwAttributes != null && rwAttributes.Length != 0)
            {
                foreach (var item in rwAttributes)
                {
                    Switch(ref flags, XBindingFlags.RWIgnoreCase, item.IgnoreCace);

                    Switch(ref flags, XBindingFlags.RWNotFoundException, item.NotFoundException);

                    Switch(ref flags, XBindingFlags.RWCannotGetException, item.CannotGetException);

                    Switch(ref flags, XBindingFlags.RWCannotSetException, item.CannotSetException);

                    Switch(ref flags, XBindingFlags.Property, item.IncludeProperties);

                    Switch(ref flags, XBindingFlags.Field, item.IncludeFields);

                    Switch(ref flags, XBindingFlags.RWSkipDefaultValue, item.SkipDefaultValue);

                    Switch(ref flags, XBindingFlags.RWMembersOptIn, item.MembersOptIn);
                }
            }

            this.type  = type;
            this.flags = flags;

            var fields     = new List <XFieldInfo>();
            var properties = new List <XPropertyInfo>();
            var indexers   = new List <XIndexerInfo>();
            var methods    = new List <XMethodInfo>();
            var rwFields   = new List <IXFieldRW>();

            GetItems(type, flags, fields, properties, indexers, methods);

            foreach (var item in fields)
            {
                if (item is IXFieldRW rwField)
                {
                    var attributes = item.FieldInfo.GetCustomAttributes(typeof(RWFieldAttribute), true);

                    if (attributes != null && attributes.Length != 0)
                    {
                        foreach (RWFieldAttribute attribute in attributes)
                        {
                            XAttributedFieldRW attributedFieldRW = (rwField, attribute);

                            if (attributedFieldRW.CanRead || attributedFieldRW.CanWrite)
                            {
                                rwFields.Add(attributedFieldRW);
                            }
                        }
                    }
                    else
                    {
                        rwFields.Add(rwField);
                    }
                }
            }

            foreach (var item in properties)
            {
                if (item is IXFieldRW rwField)
                {
                    var attributes = item.PropertyInfo.GetCustomAttributes(typeof(RWFieldAttribute), true);

                    if (attributes != null && attributes.Length != 0)
                    {
                        // Attributed property allow access non-Public accessor.
                        item.Initialize(item.propertyInfo, item.flags | XBindingFlags.NonPublic);

                        foreach (RWFieldAttribute attribute in attributes)
                        {
                            XAttributedFieldRW attributedFieldRW = (rwField, attribute);

                            if (attributedFieldRW.CanRead || attributedFieldRW.CanWrite)
                            {
                                rwFields.Add(attributedFieldRW);
                            }
                        }
                    }
                    else
                    {
                        rwFields.Add(rwField);
                    }
                }
            }

            rwFields.Sort((x, y) => x.Order.CompareTo(y.Order));

            if (rwAttributes != null && rwAttributes.Length != 0)
            {
                var temp = rwFields.Cast <IObjectField>().ToList();

                foreach (var item in rwAttributes)
                {
                    item.OnCreate(type, ref temp);
                }

                rwFields = temp.Cast <IXFieldRW>().ToList();
            }

            this.fields     = fields.ToArray();
            this.properties = properties.ToArray();
            this.indexers   = indexers.ToArray();
            this.methods    = methods.ToArray();
            this.rwFields   = rwFields.ToArray();

            fieldsCache     = new NameCache <XFieldInfo>();
            propertiesCache = new NameCache <XPropertyInfo>();
            indexersCache   = new HashCache <RuntimeParamsSign, XIndexerInfo>();
            methodsCache    = new HashCache <RuntimeMethodSign, XMethodInfo>();
            rwFieldsCache   = new RWNameCache <IXFieldRW>(flags);

            foreach (var item in fields)
            {
                fieldsCache[item.name] = item;
            }
            foreach (var item in properties)
            {
                propertiesCache[item.name] = item;
            }
            foreach (var item in indexers)
            {
                indexersCache[ParametersToTypes(item.PropertyInfo.GetIndexParameters())] = item;
            }
            foreach (var item in methods)
            {
                methodsCache[(item.MethodInfo.Name, ParametersToTypes(item.MethodInfo.GetParameters()))] = item;
Пример #3
0
        private XTypeInfo(Type type, XBindingFlags flags)
        {
            this.type  = type;
            this.flags = flags;

            IEqualityComparer <string> rwKeyComparer;

            if ((flags & XBindingFlags.RWIgnoreCase) != 0)
            {
                rwKeyComparer = new IgnoreCaseEqualityComparer();
            }
            else
            {
                rwKeyComparer = EqualityComparer <string> .Default;
            }

            var fields     = new List <XFieldInfo>();
            var properties = new List <XPropertyInfo>();
            var indexers   = new List <XIndexerInfo>();
            var methods    = new List <XMethodInfo>();
            var rwFields   = new List <IXFieldRW>();

            GetItems(type, flags, fields, properties, indexers, methods);

            foreach (var item in fields)
            {
                var rwField = item as IXFieldRW;

                if (rwField == null)
                {
                    continue;
                }

                var attributes = item.FieldInfo.GetCustomAttributes(typeof(RWFieldAttribute), true);

                if (attributes != null && attributes.Length != 0)
                {
                    foreach (RWFieldAttribute attribute in attributes)
                    {
                        var attributedFieldRW = new XAttributedFieldRW(rwField, attribute);

                        if (attributedFieldRW.CanRead || attributedFieldRW.CanWrite)
                        {
                            rwFields.Add(attributedFieldRW);
                        }
                    }
                }
                else
                {
                    rwFields.Add(rwField);
                }
            }

            foreach (var item in properties)
            {
                var rwField = item as IXFieldRW;

                if (rwField == null)
                {
                    continue;
                }

                var attributes = item.PropertyInfo.GetCustomAttributes(typeof(RWFieldAttribute), true);

                if (attributes != null && attributes.Length != 0)
                {
                    foreach (RWFieldAttribute attribute in attributes)
                    {
                        var attributedFieldRW = new XAttributedFieldRW(rwField, attribute);

                        if (attributedFieldRW.CanRead || attributedFieldRW.CanWrite)
                        {
                            rwFields.Add(attributedFieldRW);
                        }
                    }
                }
                else
                {
                    rwFields.Add(rwField);
                }
            }

            rwFields.Sort((x, y) => x.Order.CompareTo(y.Order));

            this.fields     = fields.ToArray();
            this.properties = properties.ToArray();
            this.indexers   = indexers.ToArray();
            this.methods    = methods.ToArray();
            this.rwFields   = rwFields.ToArray();

            fieldsCache     = new NameCache <XFieldInfo>();
            propertiesCache = new NameCache <XPropertyInfo>();
            indexersCache   = new HashCache <RuntimeParamsSign, XIndexerInfo>();
            methodsCache    = new HashCache <RuntimeMethodSign, XMethodInfo>();
            rwFieldsCache   = new NameCache <IXFieldRW>();

            foreach (var item in fields)
            {
                fieldsCache[item.name] = item;
            }
            foreach (var item in properties)
            {
                propertiesCache[item.name] = item;
            }
            foreach (var item in indexers)
            {
                indexersCache[new RuntimeParamsSign(ParametersToTypes(item.PropertyInfo.GetIndexParameters()))] = item;
            }
            foreach (var item in methods)
            {
                methodsCache[new RuntimeMethodSign(item.MethodInfo.Name, ParametersToTypes(item.MethodInfo.GetParameters()))] = item;
            }
            foreach (var item in rwFields)
            {
                rwFieldsCache[item.Name] = item;
            }
        }
Пример #4
0
        private XTypeInfo(Type type, XBindingFlags flags)
        {
            Type  = type;
            Flags = flags;

            IEqualityComparer <string> rwKeyComparer;

            if ((flags & XBindingFlags.RWIgnoreCase) != 0)
            {
                rwKeyComparer = new IgnoreCaseEqualityComparer();
            }
            else
            {
                rwKeyComparer = EqualityComparer <string> .Default;
            }

            fields     = new XDictionary <string, XFieldInfo>(XDictionaryOption.AllowRepeat);
            properties = new XDictionary <string, XPropertyInfo>(XDictionaryOption.AllowRepeat);
            indexers   = new XDictionary <RuntimeParamsSign, XIndexerInfo>(XDictionaryOption.AllowRepeat);
            methods    = new XDictionary <RuntimeMethodSign, XMethodInfo>(XDictionaryOption.AllowRepeat);
            rwFields   = new Dictionary <string, IXFieldRW>(rwKeyComparer);

            GetItems(type, flags);

            var rwFieldList = new List <IXFieldRW>();

            foreach (var item in fields)
            {
                var rwField = item.Value as IXFieldRW;

                if (rwField == null)
                {
                    continue;
                }

                var attributes = item.Value.FieldInfo.GetCustomAttributes(typeof(RWFieldAttribute), true);

                if (attributes != null && attributes.Length != 0)
                {
                    foreach (RWFieldAttribute attribute in attributes)
                    {
                        var attributedFieldRW = new XAttributedFieldRW(rwField, attribute);

                        if (attributedFieldRW.CanRead || attributedFieldRW.CanWrite)
                        {
                            rwFieldList.Add(attributedFieldRW);
                        }
                    }
                }
                else
                {
                    rwFieldList.Add(rwField);
                }
            }

            foreach (var item in properties)
            {
                var rwField = item.Value as IXFieldRW;

                if (rwField == null)
                {
                    continue;
                }

                var attributes = item.Value.PropertyInfo.GetCustomAttributes(typeof(RWFieldAttribute), true);

                if (attributes != null && attributes.Length != 0)
                {
                    foreach (RWFieldAttribute attribute in attributes)
                    {
                        var attributedFieldRW = new XAttributedFieldRW(rwField, attribute);

                        if (attributedFieldRW.CanRead || attributedFieldRW.CanWrite)
                        {
                            rwFieldList.Add(attributedFieldRW);
                        }
                    }
                }
                else
                {
                    rwFieldList.Add(rwField);
                }
            }

            rwFieldList.Sort((x, y) => x.Order.CompareTo(y.Order));

            foreach (var item in rwFieldList)
            {
                rwFields[item.Name] = item;
            }

            rFields = ArrayHelper.Filter(rwFieldList, item => item.CanRead, item => item);
        }