/// <summary> /// Get all methods with the specified name. /// </summary> /// <param name="name">The method name.</param> /// <param name="searchBaseClasses">Pass <c>false</c> to NOT search base classes.</param> public List <MethodRef> GetMethods(string name, bool searchBaseClasses) { NamedCodeObjectGroup results = new NamedCodeObjectGroup(); GetMethods(name, searchBaseClasses, results); return(MethodRef.MethodRefsFromGroup(results)); }
/// <summary> /// Add a source object or group to a target object or group, converting the target into a group if necessary. /// </summary> /// <param name="target">The target object or group.</param> /// <param name="source">The source object or group.</param> public static void Add(ref object target, object source) { if (target == null) { if (source is ICollection) { if (((ICollection)source).Count > 0) { target = new NamedCodeObjectGroup(source); } } else { target = source; } } else if (source != null && !(source is ICollection && ((ICollection)source).Count == 0)) { if (!(target is NamedCodeObjectGroup)) { target = new NamedCodeObjectGroup(target); } ((NamedCodeObjectGroup)target).Add(source); } }
/// <summary> /// Get the property with the specified name. /// </summary> public override PropertyRef GetProperty(string name) { // Look for the property in the type declaration NamedCodeObjectGroup found = new NamedCodeObjectGroup(); FindInAllParts <PropertyDecl>(name, found); if (found.Count > 0) { return((PropertyRef)((PropertyDecl)found[0]).CreateRef()); } // Look for the property in any base types List <Expression> baseTypes = GetAllBaseTypes(); if (baseTypes != null) { foreach (Expression baseTypeExpression in baseTypes) { TypeRef typeRef = baseTypeExpression.SkipPrefixes() as TypeRef; if (typeRef != null) { PropertyRef propertyRef = typeRef.GetProperty(name); if (propertyRef != null) { return(propertyRef); } } } } return(null); }
/// <summary> /// Get all methods of the aliased type with the specified name. /// </summary> public void GetMethods(string name, bool searchBaseClasses, NamedCodeObjectGroup results) { TypeRef typeRef = Type; if (typeRef != null) { typeRef.GetMethods(name, searchBaseClasses, results); } }
/// <summary> /// Get all methods with the specified name, adding them to the provided NamedCodeObjectGroup. /// </summary> public virtual void GetMethods(string name, bool searchBaseClasses, NamedCodeObjectGroup results) { FindInAllParts <MethodDeclBase>(name, results); if (searchBaseClasses) { TypeRef baseRef = GetBaseType(); if (baseRef != null) { baseRef.GetMethods(name, true, results); } } }
/// <summary> /// Get the nested type with the specified name. /// </summary> public TypeRef GetNestedType(string name) { NamedCodeObjectGroup found = new NamedCodeObjectGroup(); FindInAllParts <TypeDecl>(name, found); if (found.Count > 0) { return(((TypeDecl)found[0]).CreateRef()); } TypeRef baseRef = GetBaseType(); return(baseRef != null ? baseRef.GetNestedType(name) : null); }
/// <summary> /// Get the field with the specified name. /// </summary> public virtual FieldRef GetField(string name) { NamedCodeObjectGroup found = new NamedCodeObjectGroup(); FindInAllParts <FieldDecl>(name, found); if (found.Count > 0) { return((FieldRef)((FieldDecl)found[0]).CreateRef()); } TypeRef baseRef = GetBaseType(); return(baseRef != null ? baseRef.GetField(name) : null); }
/// <summary> /// Get the method with the specified name and parameters, and of type T. /// </summary> public T GetMethod <T>(string name, params TypeRefBase[] parameterTypes) where T : MethodDeclBase { NamedCodeObjectGroup found = new NamedCodeObjectGroup(); FindInAllParts <T>(name, found); foreach (T methodDecl in found) { if (methodDecl.MatchParameters(parameterTypes)) { return(methodDecl); } } return(null); }
/// <summary> /// Find all members of the type with the specified name and of type T, including in other parts of partial types. /// </summary> protected void FindInAllParts <T>(string name, NamedCodeObjectGroup results) where T : CodeObject { if (_body != null) { _body.FindChildren <T>(name, results); } if (IsPartial) { foreach (TypeDecl otherPart in GetOtherParts()) { if (otherPart.Body != null) { otherPart.Body.FindChildren <T>(name, results); } } } }
/// <summary> /// Check if we need to remove a compiler-generated default constructor when adding a constructor. /// </summary> protected void CheckRemoveGeneratedDefaultConstructor(ConstructorDecl constructorDecl) { // If we're adding a non-static constructor, we need to remove any generated ones if (!constructorDecl.IsStatic) { NamedCodeObjectGroup constructors = GetConstructors(); if (constructors != null && constructors.Count > 0) { foreach (ConstructorDecl constructor in constructors) { if (constructor.IsGenerated && constructor.ParameterCount == 0) { // Remove via its parent in case it belongs to another part of a partial type ((BlockStatement)constructor.Parent).Body.Remove(constructor); } } } } }
/// <summary> /// Get all methods with the specified name. /// </summary> public override void GetMethods(string name, bool searchBaseClasses, NamedCodeObjectGroup results) { FindInAllParts <MethodDeclBase>(name, results); if (searchBaseClasses) { List <Expression> baseTypes = GetAllBaseTypes(); if (baseTypes != null) { foreach (Expression baseTypeExpression in baseTypes) { TypeRef typeRef = baseTypeExpression.SkipPrefixes() as TypeRef; if (typeRef != null) { typeRef.GetMethods(name, true, results); } } } } }
/// <summary> /// Get all non-static constructors for this type. /// </summary> public virtual NamedCodeObjectGroup GetConstructors(bool currentPartOnly) { NamedCodeObjectGroup constructors = new NamedCodeObjectGroup(); NamedCodeObjectGroup found = new NamedCodeObjectGroup(); if (currentPartOnly && _body != null) { _body.FindChildren <ConstructorDecl>(_name, found); } else { FindInAllParts <ConstructorDecl>(_name, found); } foreach (INamedCodeObject namedCodeObject in found) { if (!((ConstructorDecl)namedCodeObject).IsStatic) { constructors.Add(namedCodeObject); } } return(constructors); }
/// <summary> /// Add a source object or group to a target object or group, converting the target into a group if necessary. /// </summary> /// <param name="target">The target object or group.</param> /// <param name="source">The source object or group.</param> public static void Add(ref INamedCodeObject target, INamedCodeObject source) { if (target == null) { if (source is NamedCodeObjectGroup) { target = new NamedCodeObjectGroup(source); } else { target = source; } } else if (source != null) { if (!(target is NamedCodeObjectGroup)) { target = new NamedCodeObjectGroup(target); } ((NamedCodeObjectGroup)target).Add(source); } }
/// <summary> /// Remove the object with the specified name from the dictionary. /// </summary> /// <param name="name">The name of the object.</param> /// <param name="namedCodeObject">The named code object.</param> public void Remove(string name, INamedCodeObject namedCodeObject) { INamedCodeObject existingObj; if (_dictionary.TryGetValue(name, out existingObj)) { if (existingObj is NamedCodeObjectGroup) { // If there's a group with the given name, remove the object from the group NamedCodeObjectGroup group = (NamedCodeObjectGroup)existingObj; group.Remove(namedCodeObject); if (group.Count == 1) { // If only one object is left in the group, replace the group with the object _dictionary.Remove(name); _dictionary.Add(name, namedCodeObject); } } else { _dictionary.Remove(name); } } }
/// <summary> /// Does nothing. /// </summary> public void GetMethods(string name, bool searchBaseClasses, NamedCodeObjectGroup results) { }
/// <summary> /// Check if we need to create or remove a compiler-generated default constructor. /// </summary> public void CheckGeneratedDefaultConstructor(bool currentPartOnly) { NamedCodeObjectGroup constructors = GetConstructors(); // Add or remove compiler-generated default constructors as necessary if (constructors == null || constructors.Count == 0) { // Add a compiler-generated default public constructor if we don't have any constructors yet, // and this isn't a static class. if (!IsStatic) { base.Add(new ConstructorDecl(Modifiers.Public) { IsGenerated = true, IsSingleLine = true }); } } else if (constructors.Count > 1) { // Remove any duplicate compiler-generated default constructors (can occur for partial types during multithreaded // parsing), and if we have any other non-static constructors, then remove all default constructors. bool removeAllDefaults = false; ConstructorDecl defaultConstructor = null; foreach (ConstructorDecl constructor in constructors) { ConstructorDecl removeConstructor = null; if (constructor.IsGenerated && constructor.ParameterCount == 0) { if (removeAllDefaults) { removeConstructor = constructor; } else if (defaultConstructor != null) { if (constructor.Parent == this) { removeConstructor = constructor; } else { removeConstructor = defaultConstructor; defaultConstructor = constructor; } } else { defaultConstructor = constructor; } } else if (!constructor.IsStatic) { removeAllDefaults = true; if (defaultConstructor != null) { removeConstructor = defaultConstructor; } } if (removeConstructor != null) { // Don't remove if we're doing the current part only and it belongs to another part if (!currentPartOnly || removeConstructor.Parent == this) { // Remove via its parent in case it belongs to another part of a partial type ((BlockStatement)removeConstructor.Parent).Body.Remove(removeConstructor); } } } } }