Ejemplo n.º 1
0
        void AddType(ITypeDefinition typeDefinition)
        {
            if (typeDefinition == null)
            {
                throw new ArgumentNullException("typeDefinition");
            }
            if (typeDefinition.ProjectContent != this)
            {
                throw new ArgumentException("Cannot add a type definition that belongs to another project content");
            }

            ITypeDefinition existingTypeDef = types.GetTypeDefinition(typeDefinition.Namespace, typeDefinition.Name, typeDefinition.TypeParameterCount, StringComparer.Ordinal);

            if (existingTypeDef != null)
            {
                // Add a part to a compound class
                var newParts = new List <ITypeDefinition>(existingTypeDef.GetParts());
                newParts.Add(typeDefinition);
                types.UpdateType(CompoundTypeDefinition.Create(newParts));
            }
            else
            {
                types.UpdateType(typeDefinition);
            }
        }
        void RemoveType(ITypeDefinition typeDefinition)
        {
            var compoundTypeDef = typeDefinition.GetDefinition() as CompoundTypeDefinition;

            if (compoundTypeDef != null)
            {
                // Remove one part from a compound class
                var newParts = new List <ITypeDefinition>(compoundTypeDef.GetParts());
                // We cannot use newParts.Remove() because we need to use reference equality
                for (int i = 0; i < newParts.Count; i++)
                {
                    if (newParts[i] == typeDefinition)
                    {
                        newParts.RemoveAt(i);
                        ((DefaultTypeDefinition)typeDefinition).SetCompoundTypeDefinition(typeDefinition);
                        break;
                    }
                }
                types.UpdateType(CompoundTypeDefinition.Create(newParts));
            }
            else
            {
                types.RemoveType(typeDefinition);
            }
        }
Ejemplo n.º 3
0
        void RemoveType(ITypeDefinition typeDefinition)
        {
            var compoundTypeDef = typeDefinition.GetDefinition() as CompoundTypeDefinition;

            if (compoundTypeDef != null)
            {
                // Remove one part from a compound class
                var newParts = new List <ITypeDefinition>(compoundTypeDef.GetParts());
                if (newParts.Remove(typeDefinition))
                {
                    ((DefaultTypeDefinition)typeDefinition).SetCompoundTypeDefinition(typeDefinition);
                }
                types.UpdateType(CompoundTypeDefinition.Create(newParts));
            }
            else
            {
                types.RemoveType(typeDefinition);
            }
        }
        public static ITypeDefinition Create(IList <ITypeDefinition> parts)
        {
            if (parts == null || parts.Count == 0)
            {
                throw new ArgumentException("parts");
            }

            ITypeDefinition mainPart = parts[0];

            for (int i = 1; i < parts.Count; i++)
            {
                if (PreferAsMainPart(parts[i], mainPart))
                {
                    mainPart = parts[i];
                }
            }
            if (parts.Count == 1)
            {
                ((DefaultTypeDefinition)mainPart).SetCompoundTypeDefinition(mainPart);
                return(mainPart);
            }

            CompoundTypeDefinition compound;

            if (mainPart.DeclaringTypeDefinition != null)
            {
                throw new NotImplementedException("nested compound types not implemented");
            }
            else
            {
                compound = new CompoundTypeDefinition(mainPart.ProjectContent, mainPart.Namespace, mainPart.Name);
            }
            compound.parts      = parts;
            compound.Region     = mainPart.Region;
            compound.BodyRegion = mainPart.BodyRegion;
            compound.TypeParameters.AddRange(mainPart.TypeParameters);
            compound.IsSynthetic   = mainPart.IsSynthetic;
            compound.Accessibility = mainPart.Accessibility;

            bool allPartsFrozen = true;

            foreach (DefaultTypeDefinition part in parts)
            {
                compound.BaseTypes.AddRange(part.BaseTypes);
                compound.Attributes.AddRange(part.Attributes);
                compound.NestedTypes.AddRange(part.NestedTypes);
                compound.Methods.AddRange(part.Methods);
                compound.Properties.AddRange(part.Properties);
                compound.Events.AddRange(part.Events);
                compound.Fields.AddRange(part.Fields);

                if (part.IsAbstract)
                {
                    compound.IsAbstract = true;
                }
                if (part.IsSealed)
                {
                    compound.IsSealed = true;
                }
                if (part.IsShadowing)
                {
                    compound.IsShadowing = true;
                }
                if (part.HasExtensionMethods)
                {
                    compound.HasExtensionMethods = true;
                }
                if (part.AddDefaultConstructorIfRequired)
                {
                    compound.AddDefaultConstructorIfRequired = true;
                }

                // internal is the default, so use another part's accessibility until we find a non-internal accessibility
                if (compound.Accessibility == Accessibility.Internal)
                {
                    compound.Accessibility = part.Accessibility;
                }

                allPartsFrozen &= part.IsFrozen;
            }

            if (allPartsFrozen)
            {
                // If all parts are frozen, also freeze the compound typedef.
                compound.Freeze();
            }
            // Publish the compound class via part.compoundTypeDefinition only after it has been frozen.
            foreach (DefaultTypeDefinition part in parts)
            {
                part.SetCompoundTypeDefinition(compound);
            }

            return(compound);
        }
		public static ITypeDefinition Create(IList<ITypeDefinition> parts)
		{
			if (parts == null || parts.Count == 0)
				throw new ArgumentException("parts");
			
			ITypeDefinition mainPart = parts[0];
			for (int i = 1; i < parts.Count; i++) {
				if (PreferAsMainPart(parts[i], mainPart))
					mainPart = parts[i];
			}
			if (parts.Count == 1) {
				((DefaultTypeDefinition)mainPart).SetCompoundTypeDefinition(mainPart);
				return mainPart;
			}
			
			CompoundTypeDefinition compound;
			if (mainPart.DeclaringTypeDefinition != null) {
				throw new NotImplementedException("nested compound types not implemented");
			} else {
				compound = new CompoundTypeDefinition(mainPart.ProjectContent, mainPart.Namespace, mainPart.Name);
			}
			compound.parts = parts;
			compound.Region = mainPart.Region;
			compound.BodyRegion = mainPart.BodyRegion;
			compound.TypeParameters.AddRange(mainPart.TypeParameters);
			compound.IsSynthetic = mainPart.IsSynthetic;
			compound.Accessibility = mainPart.Accessibility;
			
			bool allPartsFrozen = true;
			foreach (DefaultTypeDefinition part in parts) {
				compound.BaseTypes.AddRange(part.BaseTypes);
				compound.Attributes.AddRange(part.Attributes);
				compound.NestedTypes.AddRange(part.NestedTypes);
				compound.Methods.AddRange(part.Methods);
				compound.Properties.AddRange(part.Properties);
				compound.Events.AddRange(part.Events);
				compound.Fields.AddRange(part.Fields);
				
				if (part.IsAbstract)
					compound.IsAbstract = true;
				if (part.IsSealed)
					compound.IsSealed = true;
				if (part.IsShadowing)
					compound.IsShadowing = true;
				if (part.HasExtensionMethods)
					compound.HasExtensionMethods = true;
				if (part.AddDefaultConstructorIfRequired)
					compound.AddDefaultConstructorIfRequired = true;
				
				// internal is the default, so use another part's accessibility until we find a non-internal accessibility
				if (compound.Accessibility == Accessibility.Internal)
					compound.Accessibility = part.Accessibility;
				
				allPartsFrozen &= part.IsFrozen;
			}
			
			if (allPartsFrozen) {
				// If all parts are frozen, also freeze the compound typedef.
				compound.Freeze();
			}
			// Publish the compound class via part.compoundTypeDefinition only after it has been frozen.
			foreach (DefaultTypeDefinition part in parts) {
				part.SetCompoundTypeDefinition(compound);
			}
			
			return compound;
		}