/// <summary> /// Returns the first <see cref="System.CodeDom.CodeTypeMember"/> with a /// name that matches the passed in name - or null if no match is found. /// </summary> /// <param name="coll">May not be null</param> /// <param name="name">May not be null or empty</param> public static CodeTypeMember FindMemberByName(this CodeTypeMemberCollection coll, string name) { coll.ThrowIfNull("coll"); name.ThrowIfNullOrEmpty(name); foreach (CodeTypeMember member in coll) { if (member.Name == name) { return(member); } } return(null); }
/// <summary> /// Returns the first <see cref="System.CodeDom.CodeTypeDeclaration"/> with a /// name that matches the passed in name - or null if no match is found. /// </summary> /// <param name="coll">May not be null</param> /// <param name="name">May not be null or empty</param> public static CodeTypeDeclaration FindTypeMemberByName(this CodeTypeMemberCollection coll, string name) { coll.ThrowIfNull("coll"); name.ThrowIfNullOrEmpty(name); foreach (CodeTypeMember member in coll) { var field = member as CodeTypeDeclaration; if (field != null && field.Name == name) { return(field); } } return(null); }
/// <summary> /// Returns the first <see cref="System.CodeDom.CodeMemberProperty"/> with a /// name that matches the passed in name - or Null if no match found. /// </summary> /// <param name="coll">May not be null</param> /// <param name="name">May not be null or empty</param> public static CodeMemberProperty FindPropertyByName(this CodeTypeMemberCollection coll, string name) { coll.ThrowIfNull("coll"); name.ThrowIfNullOrEmpty(name); foreach (CodeTypeMember member in coll) { if ((member is CodeMemberProperty) == false) { continue; } var property = (CodeMemberProperty)member; if (property.Name == name) { return(property); } } return(null); }
/// <summary> /// Returns the first <see cref="System.CodeDom.CodeMemberProperty"/> with a /// name that matches the passed in name - or null if no match is found. /// </summary> /// <param name="coll">May not be null</param> /// <param name="name">May not be null or empty</param> public static CodeMemberField FindFieldByName(this CodeTypeMemberCollection coll, string name) { coll.ThrowIfNull("coll"); name.ThrowIfNullOrEmpty(name); foreach (CodeTypeMember member in coll) { if ((member is CodeMemberField) == false) { continue; } var field = (CodeMemberField)member; if (field.Name == name) { return(field); } } return(null); }
/// <summary> /// Adds the specified members to the given class. /// Skips elements which already have been added. /// </summary> public static void AddMembersToClass(CodeTypeDeclaration classDeclaration, CodeTypeMemberCollection membersToAdd) { classDeclaration.ThrowIfNull("classDecl"); membersToAdd.ThrowIfNull("membersToAdd"); // Create a list of all used names within the current scope. IEnumerable <string> query = from CodeTypeMember m in classDeclaration.Members select m.Name; List <string> usedNames = new List <string>(query); // Create methods. foreach (CodeTypeMember member in membersToAdd) { if (!usedNames.Contains(member.Name)) { // If this member has not yet been added, add the new type. // Due to method overloads (AsStream, AsObject) it might have been added already. usedNames.Add(member.Name); classDeclaration.Members.Add(member); } } }