Example #1
0
        ///返回[枚举项1](连接符)[枚举项2](连接符)[枚举项2]这样的格式
        public static string GetMaskEnumComment(Enum value, string connecter)
        {
            int maskValue = (int)(object)value;

            StringBuilder sb = new StringBuilder("");

            FieldInfo[] fields       = value.GetType().GetFields(BindingFlags.Static | BindingFlags.Public);
            bool        isEveryThing = true;

            for (int i = 0; i < fields.Length; i++)
            {
                CommentAttribute attr = fields[i].GetCustomAttributes(typeof(CommentAttribute), false).FirstOrDefault() as CommentAttribute;
                string           text = attr != null ? attr.Comment : fields[i].Name;
                int itemValue         = (int)fields[i].GetValue(null);
                if ((itemValue & maskValue) != 0)
                {
                    if (sb.Length > 0)
                    {
                        sb.Append(connecter);
                    }
                    sb.Append(text);
                }
                else
                {
                    isEveryThing = false;
                }
            }

            if (isEveryThing)
            {
                return("EveryThing");
            }
            return(sb.ToString());
        }
        //-------------------------------------------------------------------------------------
        public override void OnGUI(Rect position)
        {
            CommentAttribute comment_attribute = (CommentAttribute)attribute;

            EditorGUI.BeginDisabledGroup(true);

            EditorGUI.LabelField(position, comment_attribute.Content);

            EditorGUI.EndDisabledGroup();
        }
Example #3
0
        public static string GetEnumComment(Type enumType, Enum value)
        {
            var fieldInfo = enumType.GetField(value.ToString(), BindingFlags.Static | BindingFlags.Public);

            if (fieldInfo != null)
            {
                CommentAttribute attr = fieldInfo.GetCustomAttributes(typeof(CommentAttribute), false).FirstOrDefault() as CommentAttribute;
                return(attr != null ? attr.Comment : null);
            }
            return(null);
        }
        public void GivenCommentAttributeWithName_WhenSearchingByName_ShouldNotThrow()
        {
            NewElement element   = AttributesMapperSettersTests.InitializeEmptyNewElement();
            string     fieldName = "WFD_Comment1";
            var        att       = new CommentAttribute()
            {
                FieldName = fieldName
            };

            element.CommentAttributes.Add(att);

            Action a = () => AttributesMapper.GetAttributeByName <CommentAttribute>(element, fieldName);

            a.Should().NotThrow <FieldNotFoundException>();
        }
        public void GivenCommentAttributeWithName_WhenSearchingByName_ShouldBeSameAsTheAddedOne()
        {
            NewElement element   = AttributesMapperSettersTests.InitializeEmptyNewElement();
            string     fieldName = "WFD_Comment1";
            var        att       = new CommentAttribute()
            {
                FieldName = fieldName
            };

            element.CommentAttributes.Add(att);

            var foundAttribute = AttributesMapper.GetAttributeByName <CommentAttribute>(element, fieldName);

            foundAttribute.Should().Be(att);
        }
        public void GivenCommentAttributeWithId_WhenSearchingById_ShouldBeSameAsTheAddedOne()
        {
            NewElement element = AttributesMapperSettersTests.InitializeEmptyNewElement();
            int        id      = 5;
            var        att     = new CommentAttribute()
            {
                Id = id
            };

            element.CommentAttributes.Add(att);

            var foundAttribute = AttributesMapper.GetAttributeById <CommentAttribute>(element, id);

            foundAttribute.Should().Be(att);
        }
        public void GivenCommentAttributeWithId_WhenSearchingById_ShouldNotThrow()
        {
            NewElement element = AttributesMapperSettersTests.InitializeEmptyNewElement();
            int        id      = 5;
            var        att     = new CommentAttribute()
            {
                Id = id
            };

            element.CommentAttributes.Add(att);

            Action a = () => AttributesMapper.GetAttributeById <CommentAttribute>(element, id);

            a.Should().NotThrow <FieldNotFoundException>();
        }
Example #8
0
    void Start()
    {
        // On cherche l'info sur le type PlayerController
        Type monType = typeof(PlayerController);

        // On cherche l'info sur le field playerID
        FieldInfo monField = monType.GetField("playerID");

        // On va chercher tous les attributs de type CommentAttribute sur le field playerID
        object[] attributes = monField.GetCustomAttributes(typeof(CommentAttribute), false);

        // On cast le premier attribut en CommentAttribute
        CommentAttribute monAttribut = (CommentAttribute)attributes.FirstOrDefault(); // ou: as CommentAttribute;

        Debug.Log(monAttribut.Comment);
    }
Example #9
0
    private void Start()
    {
        //On vas chercher l'information sur le type player controller
        Type monType = typeof(PlayerController);

        //On vas chercher l'information sur le field player id
        FieldInfo monField = monType.GetField("playerId");

        //On vas chercher tout les attributes de type CommentAttribute sur le field playerId
        object[] attributes = monField.GetCustomAttributes(typeof(CommentAttribute), false);

        // On cast le premier attribut en CommentAttribute
        CommentAttribute monAttribut = attributes.FirstOrDefault() as CommentAttribute;

        Debug.Log(monAttribut.Comment);
    }
Example #10
0
        /// <summary>
        ///     converts object to TS interface declaration
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        protected internal string GetTypeInterface(Type t, string typeName, string extends)
        {
            if (t == typeof(Type))
            {
                throw new ArgumentException("Can not generate interface for a System.Type");
            }

            string name = typeName;

            if (string.IsNullOrEmpty(typeName))
            {
                name = RegisterType(t);
            }

            if (_tsTypes.ContainsKey(name))
            {
                return(_tsTypes[name]);
            }

            CommentAttribute commentAttr = t.GetCustomAttributes(typeof(CommentAttribute), false).OfType <CommentAttribute>().FirstOrDefault();

            StringBuilder sb = new StringBuilder();

            if (commentAttr != null && !string.IsNullOrWhiteSpace(commentAttr.Text))
            {
                AddComment(sb, commentAttr.Text);
            }
            sb.AppendFormat("export interface {0}", name);
            if (!string.IsNullOrWhiteSpace(extends))
            {
                sb.Append(" ");
                sb.Append(extends);
            }
            sb.AppendLine();
            sb.AppendLine("{");
            System.Reflection.PropertyInfo[] objProps = t.GetProperties();
            foreach (System.Reflection.PropertyInfo propInfo in objProps)
            {
                sb.AppendFormat("\t{0}{1}:{2};", propInfo.CanWrite ? "" : "readonly ", propInfo.Name, RegisterType(propInfo.PropertyType));
                sb.AppendLine();
            }
            sb.AppendLine("}");
            _tsTypes.Add(name, sb.ToString());
            _onClientTypeAdded(t);
            return(_tsTypes[name]);
        }
Example #11
0
        /// <summary>
        ///     converts object to TS enum
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        protected internal string GetTSEnum(Type t, string typeName)
        {
            string name = typeName;

            if (string.IsNullOrEmpty(typeName))
            {
                name = RegisterType(t);
            }

            if (_tsTypes.ContainsKey(name))
            {
                return(_tsTypes[name]);
            }

            CommentAttribute commentAttr =
                t.GetCustomAttributes(typeof(CommentAttribute), false).OfType <CommentAttribute>().FirstOrDefault();

            StringBuilder sb = new StringBuilder();

            if (commentAttr != null && !string.IsNullOrWhiteSpace(commentAttr.Text))
            {
                AddComment(sb, commentAttr.Text);
            }
            sb.AppendFormat("export enum {0}", name);
            sb.AppendLine();
            sb.AppendLine("{");
            int[] enumVals = Enum.GetValues(t).Cast <int>().ToArray();
            bool  isFirst  = true;

            Array.ForEach(enumVals, val =>
            {
                if (!isFirst)
                {
                    sb.AppendLine(",");
                }

                string valname = Enum.GetName(t, val);
                sb.AppendFormat("\t{0}={1}", valname, val);
                isFirst = false;
            }
                          );
            sb.AppendLine();
            sb.AppendLine("}");
            _tsTypes.Add(name, sb.ToString());
            return(_tsTypes[name]);
        }
        public void AddComment_AddsComment()
        {
            NewElement element = InitializeEmptyNewElement();
            int        id      = 15;
            var        att     = new CommentAttribute()
            {
                Id = id
            };

            att.Value = new Comments();
            element.CommentAttributes.Add(att);
            string value = "test string";

            element.AddComment(id, value);

            Assert.Equal(value, att.Value.NewComment);
        }
Example #13
0
        private void BuildCollection(Row[] rows, FieldInfo field, Type keyType, Type valueType)
        {
            uint color = selectedColor * 2 + 6;

            selectedColor = (uint)((selectedColor + 1) % groupColors.Length);

            CommentAttribute comment = TypeUtil.GetAttribute <CommentAttribute>(field);

            BuildStartColumn(false, rows, comment != null ? comment.Comment : "", field.Name, color);
            for (int i = 0; i < 1; i++)
            {
                if (keyType != null)
                {
                    BuildClass(rows, null, keyType);
                }
                BuildClass(rows, null, valueType);
            }
            BuildEndColumn(false, rows, field.Name, color + 1);
        }
Example #14
0
        private void BuildClass(Row[] rows, FieldInfo field, Type type)
        {
            CommentAttribute comment = (field != null ? TypeUtil.GetAttribute <CommentAttribute>(field) : null)
                                       ?? TypeUtil.GetAttribute <CommentAttribute>(type);

            if (type == typeof(string) || customAtomType.Contains(type) ||
                type.IsGenericType && type.GetGenericTypeDefinition() == typeof(SerializableEnum <>) || TypeUtil.IsEnum(type) || TypeUtil.IsPrimitive(type))
            {
                if (type.IsEnum && !enums.Contains(type))
                {
                    enums.Add(type);
                }
                if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(SerializableEnum <>))
                {
                    Type enumType = type.GetGenericArguments().FirstOrDefault();
                    if (!enums.Contains(enumType))
                    {
                        enums.Add(enumType);
                    }
                }
                string head = comment != null ? comment.Comment : "";

                int max = 0;
                rows[0].AppendChild(CreateCell(head, 2, ref max));
                rows[1].AppendChild(CreateCell(field != null ? field.Name : GetFieldName(type), 3, ref max));
                rows[2].AppendChild(CreateCell(GetTypeName(type), 3, ref max));
                columnWidthAndStyle.Add(new KeyValuePair <int, uint>(max, 1));
            }
            else
            {
                uint color = selectedColor * 2 + 6;
                selectedColor = (uint)((selectedColor + 1) % groupColors.Length);
                BuildStartColumn(true, rows, comment != null ? comment.Comment : "", field != null ? field.Name : type.Name, color);
                BuildHead(rows, type);
                BuildEndColumn(true, rows, field != null ? field.Name : type.Name, color + 1);
            }
        }
Example #15
0
        /// <summary>
        /// Writes the fields contained in this configuration section to the given JsonTextWriter
        /// </summary>
        /// <param name="writer">The JsonTextWriter to write the section to.</param>
        /// <param name="containingProperty">The PropertyInfo object of the ConfigFile property that contains this section.</param>
        public void Write(JsonTextWriter writer, PropertyInfo containingProperty)
        {
            string indentation = new(writer.IndentChar, writer.Indentation * 2);

            writer.WritePropertyName(containingProperty.Name);
            writer.WriteStartObject();

            //Get all fields this configuration class contains
            foreach (PropertyInfo P in GetType().GetProperties())
            {
                //If this field has a comment attribute, add a comment now.
                CommentAttribute Attr = P.GetCustomAttribute <CommentAttribute>();
                if (Attr != null)
                {
                    writer.WriteWhitespace($"\n{indentation}");
                    writer.WriteComment(Attr.Comment.Replace("\n", $"\n{indentation}"));
                }

                //Write the field. If the field is an IList, write it as a JSON array instead of a regular field.
                writer.WritePropertyName(P.Name);
                if (P.PropertyType.IsGenericType && P.PropertyType.GetGenericTypeDefinition() == typeof(List <>))
                {
                    writer.WriteStartArray();
                    var L = (IList)P.GetValue(this);
                    foreach (object Entry in L)
                    {
                        writer.WriteValue(Entry);
                    }
                    writer.WriteEndArray();
                }
                else
                {
                    writer.WriteValue(P.GetValue(this));
                }
            }
            writer.WriteEndObject();
        }
Example #16
0
 static EnumValue[] GetValues()
 {
     ConstantUtils.VerifyMask <EncodingKind>((uint)Enum.EncodingMask);
     return(typeof(Enum).GetFields().Where(a => a.IsLiteral).Select(a => new EnumValue((uint)(Enum)a.GetValue(null) !, a.Name, CommentAttribute.GetDocumentation(a))).ToArray());
 }
Example #17
0
        static EnumValue[] GetValues()
        {
            var result = typeof(Mnemonic).GetFields().Where(a => a.IsLiteral).Select(a => new EnumValue((uint)(Mnemonic)a.GetValue(null) !, a.Name, CommentAttribute.GetDocumentation(a))).ToArray();

            Array.Sort(result, (a, b) => {
                if (a == b)
                {
                    return(0);
                }
                if (a.RawName == nameof(Mnemonic.INVALID))
                {
                    return(-1);
                }
                if (b.RawName == nameof(Mnemonic.INVALID))
                {
                    return(1);
                }
                return(StringComparer.OrdinalIgnoreCase.Compare(a.RawName, b.RawName));
            });

            return(result);
        }
Example #18
0
 static EnumValue[] GetValues()
 {
     ConstantUtils.VerifyMask <SizeOverride>((uint)Enum.SizeOverrideMask);
     ConstantUtils.VerifyMask <BranchSizeInfo>((uint)Enum.BranchSizeInfoMask);
     ConstantUtils.VerifyMask <SignExtendInfo>((uint)Enum.SignExtendInfoMask);
     ConstantUtils.VerifyMask <MemorySizeInfo>((uint)Enum.MemorySizeInfoMask);
     ConstantUtils.VerifyMask <FarMemorySizeInfo>((uint)Enum.FarMemorySizeInfoMask);
     ConstantUtils.VerifyMask <MemorySize>((uint)Enum.MemorySizeMask);
     return(typeof(Enum).GetFields().Where(a => a.IsLiteral).Select(a => new EnumValue((uint)(Enum)a.GetValue(null) !, a.Name, CommentAttribute.GetDocumentation(a))).ToArray());
 }
Example #19
0
 static EnumValue[] GetValues()
 {
     if ((uint)EncoderFlags.VvvvvShift + 5 > 32)
     {
         throw new InvalidOperationException();
     }
     return(typeof(EncoderFlags).GetFields().Where(a => a.IsLiteral).Select(a => new EnumValue((uint)(EncoderFlags)a.GetValue(null) !, a.Name, CommentAttribute.GetDocumentation(a))).ToArray());
 }
Example #20
0
 static EnumValue[] GetValues()
 {
     ConstantUtils.VerifyMask <Code>((uint)Enum.CodeMask);
     ConstantUtils.VerifyMask <RoundingControl>((uint)Enum.RoundingControlMask);
     ConstantUtils.VerifyMask((uint)Enum.InstrLengthMask, IcedConstants.MaxInstructionLength);
     return(typeof(Enum).GetFields().Where(a => a.IsLiteral).Select(a => new EnumValue((uint)(Enum)a.GetValue(null) !, a.Name, CommentAttribute.GetDocumentation(a))).ToArray());
 }
Example #21
0
        /// <summary> Write a Comment XML Element from attributes in a member. </summary>
        public virtual void WriteComment(System.Xml.XmlWriter writer, System.Reflection.MemberInfo member, CommentAttribute attribute, BaseAttribute parentAttribute, System.Type mappedClass)
        {
            writer.WriteStartElement( "comment" );

            WriteUserDefinedContent(writer, member, null, attribute);

            // Write the content of this element (mixed="true")
            writer.WriteString(attribute.Content);

            writer.WriteEndElement();
        }
Example #22
0
        static Dictionary <TypeId, EnumType> CreateEnumsDict(Assembly assembly)
        {
            var allTypeIds = typeof(TypeIds).GetFields().Select(a => (TypeId)a.GetValue(null) !).ToHashSet();
            var enums      = new Dictionary <TypeId, EnumType>();

            foreach (var type in assembly.GetTypes())
            {
                var ca = type.GetCustomAttribute <EnumAttribute>(false);
                if (ca is null)
                {
                    continue;
                }
                if (!allTypeIds.Contains(ca.TypeId))
                {
                    throw new InvalidOperationException();
                }
                allTypeIds.Remove(ca.TypeId);
                var flags = EnumTypeFlags.None;
                if (ca.Public)
                {
                    flags |= EnumTypeFlags.Public;
                }
                if (ca.NoInitialize)
                {
                    flags |= EnumTypeFlags.NoInitialize;
                }
                if (ca.Flags)
                {
                    flags |= EnumTypeFlags.Flags;
                }
                var values   = type.GetFields().Where(a => a.IsLiteral).Select(a => new EnumValue(GetValue(a.GetValue(null)), a.Name, CommentAttribute.GetDocumentation(a))).ToArray();
                var enumType = new EnumType(ca.Name, ca.TypeId, ca.Documentation, values, flags);
                enums.Add(ca.TypeId, enumType);
            }
            return(enums);
        }
        //-------------------------------------------------------------------------------------
        public override float GetHeight()
        {
            CommentAttribute comment_attribute = (CommentAttribute)attribute;

            return(EditorStyles.whiteLabel.CalcHeight(comment_attribute.Content, Screen.width - 19));
        }