예제 #1
0
        protected override bool LLEquals(SwiftType other)
        {
            var bgt = other as SwiftBoundGenericType;

            return(bgt != null && BaseType.Equals(bgt.BaseType) &&
                   BoundTypes.SequenceEqual(bgt.BoundTypes));
        }
        private string SerializeValue(Type?propertyType, object?value, IEnumerable <ValidationAttribute?>?validationAttributes, PacketIndexAttribute?packetIndexAttribute = null)
        {
            if (propertyType == null && validationAttributes.All(a => a !.IsValid(value)))
            {
                return(string.Empty);
            }

            if (packetIndexAttribute?.IsOptional == true && string.IsNullOrEmpty(Convert.ToString(value)))
            {
                return(string.Empty);
            }

            // check for nullable without value or string
            if (propertyType == typeof(string) && string.IsNullOrEmpty(Convert.ToString(value)))
            {
                return($"{packetIndexAttribute?.SpecialSeparator}-");
            }

            if (propertyType != null && Nullable.GetUnderlyingType(propertyType) != null && string.IsNullOrEmpty(Convert.ToString(value)))
            {
                return($"{packetIndexAttribute?.SpecialSeparator}-1");
            }

            // enum should be casted to number
            if (propertyType !.BaseType?.Equals(typeof(Enum)) == true)
            {
                return($"{packetIndexAttribute?.SpecialSeparator}{Convert.ToInt16(value)}");
            }

            if (propertyType == typeof(bool))
            {
                // bool is 0 or 1 not True or False
                return(Convert.ToBoolean(value) ? $"{packetIndexAttribute?.SpecialSeparator}1" : $"{packetIndexAttribute?.SpecialSeparator}0");
            }

            if (value is IPacket)
            {
                PacketInformation subpacketSerializationInfo = GetSerializationInformation(value.GetType());
                return(SerializeSubpacket(value, subpacketSerializationInfo, packetIndexAttribute?.IsOptional ?? false));
            }

            if (propertyType.BaseType?.Equals(typeof(IPacket)) == true)
            {
                PacketInformation subpacketSerializationInfo = GetSerializationInformation(propertyType);
                return(SerializeSubpacket(value, subpacketSerializationInfo, packetIndexAttribute?.IsOptional ?? false));
            }

            if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition().IsAssignableFrom(typeof(List <>)) &&
                propertyType.GenericTypeArguments[0].BaseType == typeof(IPacket))
            {
                return(packetIndexAttribute?.SpecialSeparator + SerializeSubpackets((IList?)value, propertyType));
            }

            if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition().IsAssignableFrom(typeof(List <>))) //simple list
            {
                return(packetIndexAttribute?.SpecialSeparator + SerializeSimpleList((IList?)value, propertyType, packetIndexAttribute));
            }

            return($"{packetIndexAttribute?.SpecialSeparator}{value}");
        }
예제 #3
0
 public bool Equals(InterfaceDeclaration compareNode)
 {
     return
         (compareNode != null &&
          BaseType?.Equals(compareNode.BaseType) != false &&
          BaseTypeOriginalDefinition?.Equals(compareNode.BaseTypeOriginalDefinition) != false &&
          SemanticAssembly?.Equals(compareNode.SemanticAssembly) != false &&
          base.Equals(compareNode));
 }
예제 #4
0
 public bool Equals(ClassDeclaration compareNode)
 {
     return
         (compareNode != null &&
          BaseType?.Equals(compareNode.BaseType) != false &&
          BaseTypeOriginalDefinition?.Equals(compareNode.BaseTypeOriginalDefinition) != false &&
          Modifiers?.Equals(compareNode.Modifiers) != false &&
          SemanticAssembly?.Equals(compareNode.SemanticAssembly) != false &&
          base.Equals(compareNode));
 }
예제 #5
0
        private bool Expect(Position position, BaseType expected, BaseType actual)
        {
            if (expected.Equals(actual))
            {
                return(true);
            }

            Error(position, $"Expected {expected}, but got {actual}.");
            return(false);
        }
예제 #6
0
        public bool IsDescendant(XdtoValueType type)
        {
            if (BaseType == null)
            {
                return(false);
            }

            if (BaseType.Equals(type))
            {
                return(true);
            }

            return(BaseType.IsDescendant(type));
        }
예제 #7
0
        private static bool CanImplecitCast(BaseType type, BaseType cast)
        {
            if (type is IntegerType iType && cast is IntegerType iCast)
            {
                if (iType.Signed == iCast.Signed && iType.Size <= iCast.Size)
                {
                    return(true);
                }
                return(iType.Size == BaseType.UnknownSize && iCast.Signed);
            }

            if (type is FloatType fType && cast is FloatType fCast)
            {
                return(fType.Size <= fCast.Size);
            }

            return(type.Equals(cast));
        }
예제 #8
0
        /// <summary>
        /// 获取枚举的实际数据类型
        /// </summary>
        /// <param name="defineVal">定义值</param>
        /// <returns></returns>
        public object GetUnderlyingValue(object defineVal)
        {
            if (defineVal == null)
            {
                throw new ArgumentNullException("defineVal");
            }

            string valType = TypeCache.ToSimpleType(defineVal.GetType());

            if (BaseType.Equals(valType))
            {
                return(defineVal);
            }
            else
            {
                //从字符串转换为目标类型
                return(Convert.ChangeType(defineVal.ToString(), TypeCache.GetRuntimeType(BaseType)));
            }
        }
            public override object[] GetCustomAttributes(Type attributeType, bool inherit)
            {
                bool stop = this.IsGenericType;
                // Start with the table attributes.
                List <object> result = new List <object>();

                result.AddRange(ReflectionContext.Table.GetCustomAttributes(this).Where(attr => attributeType.IsAssignableFrom(attr.GetType())));

                // Then check this type, without inheritance. Add only attributes if Multiple = true OR attribute not already exists.
                foreach (var ca in base.GetCustomAttributes(attributeType, false))
                {
                    if (AttributeUtil.GetAttributeUsage(ca.GetType()).AllowMultiple || !result.Any(attr => attr.GetType().Equals(ca.GetType())))
                    {
                        result.Add(ca);
                    }
                }

                // Then get base attributes, add only if Inherit = true AND (Multiple = true OR attribute not already exists).
                if (inherit && BaseType != null && !BaseType.Equals(typeof(object)))
                {
                    foreach (var ca in BaseType.GetCustomAttributes(attributeType, inherit))
                    {
                        AttributeUsageAttribute attributeUsage = AttributeUtil.GetAttributeUsage(ca.GetType());
                        if (attributeUsage.Inherited && (attributeUsage.AllowMultiple || !result.Any(attr => attr.GetType().Equals(ca.GetType()))))
                        {
                            result.Add(ca);
                        }
                    }
                }

                object[] arrResult = (object[])Array.CreateInstance(attributeType, result.Count);
                for (int i = 0; i < result.Count; i++)
                {
                    arrResult[i] = result[i];
                }

                return(arrResult);
            }