Exemple #1
0
        internal override void Initialize(System.Reflection.FieldInfo fieldInfo, XBindingFlags flags)
        {
            base.Initialize(fieldInfo, flags);

            address = (IntPtr)((long)TypeHelper.GetTypeStaticMemoryAddress(fieldInfo.DeclaringType) + TypeHelper.OffsetOf(fieldInfo));
        }
Exemple #2
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);
        }
Exemple #3
0
 public CacheKey(Type type, XBindingFlags flags)
 {
     this.type  = type;
     this.flags = flags;
 }
Exemple #4
0
        /// <summary>
        /// 将数据源中的所有成员的名称和值写入到数据写入器中。
        /// </summary>
        /// <param name="dataWriter">数据写入器</param>
        public void OnReadAll(IDataWriter <string> dataWriter)
        {
            const XBindingFlags MembersOptInWithSkipDefaultValue = XBindingFlags.RWSkipDefaultValue | XBindingFlags.RWMembersOptIn;

            if ((xTypeInfo.flags & MembersOptInWithSkipDefaultValue) == MembersOptInWithSkipDefaultValue)
            {
                MembersOptIn_SkipDefaultValue();
            }
            else if ((xTypeInfo.flags & XBindingFlags.RWMembersOptIn) != 0)
            {
                MembersOptIn();
            }
            else if ((xTypeInfo.flags & XBindingFlags.RWSkipDefaultValue) != 0)
            {
                SkipDefaultValue();
            }
            else
            {
                None();
            }

            void MembersOptIn_SkipDefaultValue()
            {
                var valueCopyer = new ValueCopyer();

                foreach (var item in xTypeInfo.rwFields)
                {
                    if (item is XAttributedFieldRW fieldRW && fieldRW.CanRead)
                    {
                        fieldRW.OnReadValue(obj, valueCopyer);

                        if (!valueCopyer.IsEmptyValue())
                        {
                            valueCopyer.WriteTo(dataWriter[fieldRW.Name]);
                        }
                    }
                }
            }

            void MembersOptIn()
            {
                foreach (var item in xTypeInfo.rwFields)
                {
                    if (item is XAttributedFieldRW fieldRW && fieldRW.CanRead)
                    {
                        fieldRW.OnReadValue(obj, dataWriter[fieldRW.Name]);
                    }
                }
            }

            void SkipDefaultValue()
            {
                var valueCopyer = new ValueCopyer();

                foreach (var item in xTypeInfo.rwFields)
                {
                    if (item.CanRead)
                    {
                        item.OnReadValue(obj, valueCopyer);

                        if (!valueCopyer.IsEmptyValue())
                        {
                            valueCopyer.WriteTo(dataWriter[item.Name]);
                        }
                    }
                }
            }

            void None()
            {
                foreach (var item in xTypeInfo.rwFields)
                {
                    if (item.CanRead)
                    {
                        item.OnReadValue(obj, dataWriter[item.Name]);
                    }
                }
            }
        }
Exemple #5
0
        internal override void Initialize(FieldInfo fieldInfo, XBindingFlags flags)
        {
            base.Initialize(fieldInfo, flags);

            value = (TValue)FieldInfo.GetRawConstantValue();
        }
Exemple #6
0
 /// <summary>
 /// 创建 XTypeInfo 类型信息。
 /// </summary>
 /// <param name="type">需要创建 XTypeInfo 类型信息的类型</param>
 /// <param name="flags">绑定参数</param>
 /// <returns>返回一个 XTypeInfo 类型信息</returns>
 public static XTypeInfo Create(Type type, XBindingFlags flags = XBindingFlags.Default) => XCache.Get(type)[flags];
Exemple #7
0
 /// <summary>
 /// 创建 XTypeInfo 类型信息。
 /// </summary>
 /// <typeparam name="T">需要创建 XTypeInfo 类型信息的类型</typeparam>
 /// <param name="flags">绑定参数</param>
 /// <returns>返回一个 XTypeInfo 类型信息</returns>
 public static XTypeInfo Create <T>(XBindingFlags flags = XBindingFlags.Default) => XCache.Get <T>()[flags];
Exemple #8
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;
Exemple #9
0
 public XAssistant(XBindingFlags flags)
 {
     Flags = flags;
 }
Exemple #10
0
        /// <summary>
        /// 设置对象读写器接口为 XObjectInterface,并设置一个支持针对性接口的对象的默认绑定标识。
        /// </summary>
        /// <param name="targeted">支持针对性接口的对象</param>
        /// <param name="flags">默认绑定标识</param>
        public static void SetXObjectRWFlags(this ITargetedBind targeted, XBindingFlags flags)
        {
            ValueInterface.DefaultObjectInterfaceType = typeof(XObjectInterface <>);

            ValueInterface <XAssistant> .SetTargetedInterface(targeted, new XAssistant(flags));
        }
Exemple #11
0
 /// <summary>T
 /// 创建 XMethodInfo 方法信息。
 /// </summary>
 /// <param name="methodInfo">.Net 自带 MethodInfo 方法信息</param>
 /// <param name="flags">绑定标识</param>
 /// <returns>返回一个 XMethodInfo 方法信息。</returns>
 public static XMethodInfo Create(MethodInfo methodInfo, XBindingFlags flags)
 {
     return(new XMethodInfo(methodInfo, flags));
 }
 /// <summary>
 /// 创建索引器信息。
 /// </summary>
 /// <param name="propertyInfo">.Net 自带的索引器信息</param>
 /// <param name="flags">绑定标识</param>
 /// <returns>返回 XIndexerInfo 索引器信息</returns>
 public static XIndexerInfo Create(PropertyInfo propertyInfo, XBindingFlags flags = XBindingFlags.Indexer)
 {
     return(new XIndexerInfo(propertyInfo, flags));
 }