Ejemplo n.º 1
0
        public static INamedElement AddMemberToClass(Project project, ITypeDefinition cls, IUnresolvedTypeDefinition specificPartToAffect, CodeTypeMember member, bool throwIfExists)
        {
            bool isChildClass = false;

            foreach (var c in cls.Parts)
            {
                if (c == specificPartToAffect)
                {
                    isChildClass = true;
                }
            }
            if (!isChildClass)
            {
                throw new ArgumentException("Class specificPartToAffect is not a part of class cls");
            }

            var dom            = TypeSystemService.GetCompilation(project);
            var existingMember = GetCompatibleMemberInClass(dom, cls, member);

            if (existingMember == null)
            {
                return(CodeGenerationService.AddCodeDomMember(project, specificPartToAffect, member));
            }


            if (throwIfExists)
            {
                throw new MemberExistsException(cls.Name, member, MemberType.Method, existingMember.BodyRegion, cls.Region.FileName);
            }

            return(existingMember);
        }
Ejemplo n.º 2
0
        public static IMember AddMemberToClass(Project project, IType cls, IType specificPartToAffect, CodeTypeMember member, bool throwIfExists)
        {
            bool isChildClass = false;

            foreach (IType c in cls.Parts)
            {
                if (c == specificPartToAffect)
                {
                    isChildClass = true;
                }
            }
            if (!isChildClass)
            {
                throw new ArgumentException("Class specificPartToAffect is not a part of class cls");
            }

            ProjectDom dom            = ProjectDomService.GetProjectDom(project);
            IMember    existingMember = GetCompatibleMemberInClass(dom, cls, member);

            if (existingMember == null)
            {
                return(CodeGenerationService.AddCodeDomMember(specificPartToAffect, member));
            }

            if (throwIfExists)
            {
                throw new MemberExistsException(cls.Name, member, MemberType.Method, existingMember.BodyRegion, cls.CompilationUnit.FileName);
            }

            return(existingMember);
        }