Exemplo n.º 1
0
        /// <summary>
        /// 创建 XPropertyInfo 属性信息。
        /// </summary>
        /// <param name="propertyInfo">.Net 自带的 PropertyInfo 属性信息</param>
        /// <param name="flags">绑定标识</param>
        /// <returns>返回 XPropertyInfo 属性信息。</returns>
        public static XPropertyInfo Create(PropertyInfo propertyInfo, XBindingFlags flags)
        {
            if (propertyInfo is null)
            {
                throw new ArgumentNullException(nameof(propertyInfo));
            }

            var declaringType = propertyInfo.DeclaringType;

            var propertyType
                = propertyInfo.PropertyType.IsPointer ? typeof(IntPtr)
                : propertyInfo.PropertyType.IsByRef ? propertyInfo.PropertyType.GetElementType()
                : propertyInfo.PropertyType;

            XPropertyInfo result;

            try
            {
                var targetType
                    = propertyType.IsByRefLike() ? typeof(XDefaultPropertyInfo)
                     : propertyInfo.IsStatic() ? typeof(XStaticPropertyInfo <>).MakeGenericType(propertyType)
                     : declaringType.IsValueType ? typeof(XStructPropertyInfo <,>).MakeGenericType(declaringType, propertyType)
                    : typeof(XClassPropertyInfo <,>).MakeGenericType(declaringType, propertyType);

                result = (XPropertyInfo)Activator.CreateInstance(targetType, true);

                result.Initialize(propertyInfo, flags);
            }
            catch

#if DEBUG
            (Exception e)
#endif
            {
#if DEBUG
                if (e is TargetInvocationException tie)
                {
                    e = tie.InnerException;
                }

                Console.WriteLine($"{nameof(XPropertyInfo)} : Exception : {e.GetType()} -- {e.Message}");
#endif

                result = new XDefaultPropertyInfo();

                result.Initialize(propertyInfo, flags);
            }

            return(result);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 创建 XPropertyInfo 属性信息。
        /// </summary>
        /// <param name="propertyInfo">.Net 自带的 PropertyInfo 属性信息</param>
        /// <param name="flags">绑定标识</param>
        /// <returns>返回 XPropertyInfo 属性信息。</returns>
        public static XPropertyInfo Create(PropertyInfo propertyInfo, XBindingFlags flags)
        {
            if (propertyInfo is null)
            {
                throw new ArgumentNullException(nameof(propertyInfo));
            }

            var declaringType = propertyInfo.DeclaringType;

            var propertyType
                = propertyInfo.PropertyType.IsPointer ? typeof(IntPtr)
                : propertyInfo.PropertyType.IsByRef ? propertyInfo.PropertyType.GetElementType()
                : propertyInfo.PropertyType;

            XPropertyInfo result;

            try
            {
                var targetType
                    = propertyType.IsByRefLike() ? typeof(XDefaultPropertyInfo)
                     : propertyInfo.IsStatic() ? typeof(XStaticPropertyInfo <>).MakeGenericType(propertyType)
                     : declaringType.IsValueType ? typeof(XStructPropertyInfo <,>).MakeGenericType(declaringType, propertyType)
                    : typeof(XClassPropertyInfo <,>).MakeGenericType(declaringType, propertyType);

                result = (XPropertyInfo)Activator.CreateInstance(targetType, true);

                result.Initialize(propertyInfo, flags);
            }
            catch
            {
                result = new XDefaultPropertyInfo();

                result.Initialize(propertyInfo, flags);
            }

            return(result);
        }