当前位置: 首页>>代码示例>>C#>>正文


C# TypeDeclaration.AddAnnotation方法代码示例

本文整理汇总了C#中TypeDeclaration.AddAnnotation方法的典型用法代码示例。如果您正苦于以下问题:C# TypeDeclaration.AddAnnotation方法的具体用法?C# TypeDeclaration.AddAnnotation怎么用?C# TypeDeclaration.AddAnnotation使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在TypeDeclaration的用法示例。


在下文中一共展示了TypeDeclaration.AddAnnotation方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: CreateType

		public TypeDeclaration CreateType(TypeDefinition typeDef)
		{
			TypeDefinition oldCurrentType = context.CurrentType;
			context.CurrentType = typeDef;
			TypeDeclaration astType = new TypeDeclaration();
			ConvertAttributes(astType, typeDef);
			astType.AddAnnotation(typeDef);
			astType.Modifiers = ConvertModifiers(typeDef);
			astType.Name = CleanName(typeDef.Name);
			
			if (typeDef.IsEnum) {  // NB: Enum is value type
				astType.ClassType = ClassType.Enum;
				astType.Modifiers &= ~Modifiers.Sealed;
			} else if (typeDef.IsValueType) {
				astType.ClassType = ClassType.Struct;
				astType.Modifiers &= ~Modifiers.Sealed;
			} else if (typeDef.IsInterface) {
				astType.ClassType = ClassType.Interface;
				astType.Modifiers &= ~Modifiers.Abstract;
			} else {
				astType.ClassType = ClassType.Class;
			}
			
			IEnumerable<GenericParameter> genericParameters = typeDef.GenericParameters;
			if (typeDef.DeclaringType != null && typeDef.DeclaringType.HasGenericParameters)
				genericParameters = genericParameters.Skip(typeDef.DeclaringType.GenericParameters.Count);
			astType.TypeParameters.AddRange(MakeTypeParameters(genericParameters));
			astType.Constraints.AddRange(MakeConstraints(genericParameters));
			
			// Nested types
			foreach(TypeDefinition nestedTypeDef in typeDef.NestedTypes) {
				if (MemberIsHidden(nestedTypeDef, context.Settings))
					continue;
				astType.AddChild(CreateType(nestedTypeDef), TypeDeclaration.MemberRole);
			}
			
			
			if (typeDef.IsEnum) {
				long expectedEnumMemberValue = 0;
				bool forcePrintingInitializers = IsFlagsEnum(typeDef);
				foreach (FieldDefinition field in typeDef.Fields) {
					if (field.IsRuntimeSpecialName) {
						// the value__ field
						if (field.FieldType != typeDef.Module.TypeSystem.Int32) {
							astType.AddChild(ConvertType(field.FieldType), TypeDeclaration.BaseTypeRole);
						}
					} else {
						EnumMemberDeclaration enumMember = new EnumMemberDeclaration();
						enumMember.Name = CleanName(field.Name);
						long memberValue = (long)CSharpPrimitiveCast.Cast(TypeCode.Int64, field.Constant, false);
						if (forcePrintingInitializers || memberValue != expectedEnumMemberValue) {
							enumMember.AddChild(new PrimitiveExpression(field.Constant), EnumMemberDeclaration.InitializerRole);
						}
						expectedEnumMemberValue = memberValue + 1;
						astType.AddChild(enumMember, TypeDeclaration.MemberRole);
					}
				}
			} else {
				// Base type
				if (typeDef.BaseType != null && !typeDef.IsValueType && typeDef.BaseType.FullName != "System.Object") {
					astType.AddChild(ConvertType(typeDef.BaseType), TypeDeclaration.BaseTypeRole);
				}
				foreach (var i in typeDef.Interfaces)
					astType.AddChild(ConvertType(i), TypeDeclaration.BaseTypeRole);
				
				
				AddTypeMembers(astType, typeDef);
			}

			context.CurrentType = oldCurrentType;
			return astType;
		}
开发者ID:eldersantos,项目名称:ILSpy,代码行数:72,代码来源:AstBuilder.cs

示例2: CreateType

        public TypeDeclaration CreateType(TypeDefinition typeDef)
        {
            TypeDeclaration astType = new TypeDeclaration();
            astType.AddAnnotation(typeDef);
            astType.Modifiers = ConvertModifiers(typeDef);
            astType.Name = typeDef.Name;

            if (typeDef.IsEnum) {  // NB: Enum is value type
                astType.ClassType = ClassType.Enum;
                astType.Modifiers &= ~Modifiers.Sealed;
            } else if (typeDef.IsValueType) {
                astType.ClassType = ClassType.Struct;
                astType.Modifiers &= ~Modifiers.Sealed;
            } else if (typeDef.IsInterface) {
                astType.ClassType = ClassType.Interface;
                astType.Modifiers &= ~Modifiers.Abstract;
            } else {
                astType.ClassType = ClassType.Class;
            }

            astType.TypeParameters.AddRange(MakeTypeParameters(typeDef.GenericParameters));
            astType.Constraints.AddRange(MakeConstraints(typeDef.GenericParameters));

            // Nested types
            foreach(TypeDefinition nestedTypeDef in typeDef.NestedTypes) {
                if (MemberIsHidden(nestedTypeDef))
                    continue;
                astType.AddChild(CreateType(nestedTypeDef), TypeDeclaration.MemberRole);
            }

            if (typeDef.IsEnum) {
                foreach (FieldDefinition field in typeDef.Fields) {
                    if (field.IsRuntimeSpecialName) {
                        // the value__ field
                        astType.AddChild(ConvertType(field.FieldType), TypeDeclaration.BaseTypeRole);
                    } else {
                        EnumMemberDeclaration enumMember = new EnumMemberDeclaration();
                        enumMember.Name = field.Name;
                        astType.AddChild(enumMember, TypeDeclaration.MemberRole);
                    }
                }
            } else {
                // Base type
                if (typeDef.BaseType != null && !typeDef.IsValueType && typeDef.BaseType.FullName != "System.Object") {
                    astType.AddChild(ConvertType(typeDef.BaseType), TypeDeclaration.BaseTypeRole);
                }
                foreach (var i in typeDef.Interfaces)
                    astType.AddChild(ConvertType(i), TypeDeclaration.BaseTypeRole);

                AddTypeMembers(astType, typeDef);
            }

            return astType;
        }
开发者ID:petr-k,项目名称:ILSpy,代码行数:54,代码来源:AstBuilder.cs

示例3: ConvertTypeDefinition

        EntityDeclaration ConvertTypeDefinition(ITypeDefinition typeDefinition)
        {
            Modifiers modifiers = Modifiers.None;
            if (this.ShowAccessibility) {
                modifiers |= ModifierFromAccessibility(typeDefinition.Accessibility);
            }
            if (this.ShowModifiers) {
                if (typeDefinition.IsStatic) {
                    modifiers |= Modifiers.Static;
                } else if (typeDefinition.IsAbstract) {
                    modifiers |= Modifiers.Abstract;
                } else if (typeDefinition.IsSealed) {
                    modifiers |= Modifiers.Sealed;
                }
                if (typeDefinition.IsShadowing) {
                    modifiers |= Modifiers.New;
                }
            }

            ClassType classType;
            switch (typeDefinition.Kind) {
                case TypeKind.Struct:
                    classType = ClassType.Struct;
                    modifiers &= ~Modifiers.Sealed;
                    break;
                case TypeKind.Enum:
                    classType = ClassType.Enum;
                    modifiers &= ~Modifiers.Sealed;
                    break;
                case TypeKind.Interface:
                    classType = ClassType.Interface;
                    modifiers &= ~Modifiers.Abstract;
                    break;
                case TypeKind.Delegate:
                    IMethod invoke = typeDefinition.GetDelegateInvokeMethod();
                    if (invoke != null) {
                        return ConvertDelegate(invoke, modifiers);
                    } else {
                        goto default;
                    }
                default:
                    classType = ClassType.Class;
                    break;
            }

            var decl = new TypeDeclaration();
            decl.ClassType = classType;
            decl.Modifiers = modifiers;
            if (ShowAttributes) {
                decl.Attributes.AddRange (typeDefinition.Attributes.Select ((a) => new AttributeSection (ConvertAttribute (a))));
            }
            if (AddResolveResultAnnotations) {
                decl.AddAnnotation(new TypeResolveResult(typeDefinition));
            }
            decl.Name = typeDefinition.Name;

            int outerTypeParameterCount = (typeDefinition.DeclaringTypeDefinition == null) ? 0 : typeDefinition.DeclaringTypeDefinition.TypeParameterCount;

            if (this.ShowTypeParameters) {
                foreach (ITypeParameter tp in typeDefinition.TypeParameters.Skip(outerTypeParameterCount)) {
                    decl.TypeParameters.Add(ConvertTypeParameter(tp));
                }
            }

            if (this.ShowBaseTypes) {
                foreach (IType baseType in typeDefinition.DirectBaseTypes) {
                    if (baseType.IsKnownType (KnownTypeCode.Enum)) {
                        if (!typeDefinition.EnumUnderlyingType.IsKnownType (KnownTypeCode.Int32)) {
                            decl.BaseTypes.Add (ConvertType (typeDefinition.EnumUnderlyingType));
                        }
                    } else if (!baseType.IsKnownType (KnownTypeCode.Object) &&
                         !baseType.IsKnownType (KnownTypeCode.ValueType)) {
                        decl.BaseTypes.Add (ConvertType (baseType));
                    }
                }
            }

            if (this.ShowTypeParameters && this.ShowTypeParameterConstraints) {
                foreach (ITypeParameter tp in typeDefinition.TypeParameters.Skip(outerTypeParameterCount)) {
                    var constraint = ConvertTypeParameterConstraint(tp);
                    if (constraint != null)
                        decl.Constraints.Add(constraint);
                }
            }
            return decl;
        }
开发者ID:yyp2003net,项目名称:NRefactory,代码行数:86,代码来源:TypeSystemAstBuilder.cs

示例4: CreateType

		/// <summary>
		/// Creates the AST for a type definition.
		/// </summary>
		/// <param name="typeDef"></param>
		/// <returns>TypeDeclaration or DelegateDeclaration.</returns>
		public AttributedNode CreateType(TypeDefinition typeDef)
		{
			// create CSharp code mappings - used for debugger
			if (this.CodeMappings == null)
				this.CodeMappings = new Tuple<string, List<MemberMapping>>(typeDef.FullName, new List<MemberMapping>());
			
			// create type
			TypeDefinition oldCurrentType = context.CurrentType;
			context.CurrentType = typeDef;
			TypeDeclaration astType = new TypeDeclaration();
			ConvertAttributes(astType, typeDef);
			astType.AddAnnotation(typeDef);
			astType.Modifiers = ConvertModifiers(typeDef);
			astType.Name = CleanName(typeDef.Name);
			
			if (typeDef.IsEnum) {  // NB: Enum is value type
				astType.ClassType = ClassType.Enum;
				astType.Modifiers &= ~Modifiers.Sealed;
			} else if (typeDef.IsValueType) {
				astType.ClassType = ClassType.Struct;
				astType.Modifiers &= ~Modifiers.Sealed;
			} else if (typeDef.IsInterface) {
				astType.ClassType = ClassType.Interface;
				astType.Modifiers &= ~Modifiers.Abstract;
			} else {
				astType.ClassType = ClassType.Class;
			}
			
			IEnumerable<GenericParameter> genericParameters = typeDef.GenericParameters;
			if (typeDef.DeclaringType != null && typeDef.DeclaringType.HasGenericParameters)
				genericParameters = genericParameters.Skip(typeDef.DeclaringType.GenericParameters.Count);
			astType.TypeParameters.AddRange(MakeTypeParameters(genericParameters));
			astType.Constraints.AddRange(MakeConstraints(genericParameters));
			
			// Nested types
			foreach (TypeDefinition nestedTypeDef in typeDef.NestedTypes) {
				if (MemberIsHidden(nestedTypeDef, context.Settings))
					continue;
				astType.AddChild(CreateType(nestedTypeDef), TypeDeclaration.MemberRole);
			}
			
			AttributedNode result = astType;
			if (typeDef.IsEnum) {
				long expectedEnumMemberValue = 0;
				bool forcePrintingInitializers = IsFlagsEnum(typeDef);
				foreach (FieldDefinition field in typeDef.Fields) {
					if (field.IsRuntimeSpecialName) {
						// the value__ field
						if (field.FieldType != typeDef.Module.TypeSystem.Int32) {
							astType.AddChild(ConvertType(field.FieldType), TypeDeclaration.BaseTypeRole);
						}
					} else {
						EnumMemberDeclaration enumMember = new EnumMemberDeclaration();
						enumMember.Name = CleanName(field.Name);
						long memberValue = (long)CSharpPrimitiveCast.Cast(TypeCode.Int64, field.Constant, false);
						if (forcePrintingInitializers || memberValue != expectedEnumMemberValue) {
							enumMember.AddChild(new PrimitiveExpression(field.Constant), EnumMemberDeclaration.InitializerRole);
						}
						expectedEnumMemberValue = memberValue + 1;
						astType.AddChild(enumMember, TypeDeclaration.MemberRole);
					}
				}
			} else if (typeDef.BaseType != null && typeDef.BaseType.FullName == "System.MulticastDelegate") {
				DelegateDeclaration dd = new DelegateDeclaration();
				dd.Modifiers = astType.Modifiers & ~Modifiers.Sealed;
				dd.Name = astType.Name;
				dd.AddAnnotation(typeDef);
				astType.Attributes.MoveTo(dd.Attributes);
				astType.TypeParameters.MoveTo(dd.TypeParameters);
				astType.Constraints.MoveTo(dd.Constraints);
				foreach (var m in typeDef.Methods) {
					if (m.Name == "Invoke") {
						dd.ReturnType = ConvertType(m.ReturnType, m.MethodReturnType);
						dd.Parameters.AddRange(MakeParameters(m));
						ConvertAttributes(dd, m.MethodReturnType, m.Module);
					}
				}
				result = dd;
			} else {
				// Base type
				if (typeDef.BaseType != null && !typeDef.IsValueType && typeDef.BaseType.FullName != "System.Object") {
					astType.AddChild(ConvertType(typeDef.BaseType), TypeDeclaration.BaseTypeRole);
				}
				foreach (var i in typeDef.Interfaces)
					astType.AddChild(ConvertType(i), TypeDeclaration.BaseTypeRole);
				
				AddTypeMembers(astType, typeDef);

				if (astType.Members.OfType<IndexerDeclaration>().Any(idx => idx.PrivateImplementationType.IsNull)) {
					// Remove the [DefaultMember] attribute if the class contains indexers
					foreach (AttributeSection section in astType.Attributes) {
						foreach (Ast.Attribute attr in section.Attributes) {
							TypeReference tr = attr.Type.Annotation<TypeReference>();
							if (tr != null && tr.Name == "DefaultMemberAttribute" && tr.Namespace == "System.Reflection") {
								attr.Remove();
//.........这里部分代码省略.........
开发者ID:ThomasZitzler,项目名称:ILSpy,代码行数:101,代码来源:AstBuilder.cs


注:本文中的TypeDeclaration.AddAnnotation方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。