private static IEnumerable <TSymbol> TryFindMembersInThisOrBaseTypes <TSymbol>( INamedTypeSymbol containingType, ITypeSymbol type, string memberName) where TSymbol : class, ISymbol { var methods = type.GetAccessibleMembersInThisAndBaseTypes <TSymbol>(containingType); return(methods.Where(m => m.Name == memberName)); }
public static bool CanSupportCollectionInitializer(this ITypeSymbol typeSymbol, ISymbol within) { return (typeSymbol.AllInterfaces.Any(i => i.SpecialType == SpecialType.System_Collections_IEnumerable) && typeSymbol.GetAccessibleMembersInThisAndBaseTypes <IMethodSymbol>(within ?? typeSymbol).Where(s => s.Name == WellKnownMemberNames.CollectionInitializerAddMethodName) .OfType <IMethodSymbol>() .Any(m => m.Parameters.Any())); }
// static DomRegion GetRegion (INamedElement el) // { // if (el is IEntity) // return ((IEntity)el).BodyRegion; // return ((IUnresolvedEntity)el).BodyRegion; // } // //opens the code view with the desired method, creating it if it doesn't already exist // public static void CreateAndShowMember (Project project, ITypeDefinition cls, IUnresolvedTypeDefinition specificPartToAffect, CodeTypeMember member) // { // //only adds the method if it doesn't already exist // var mem = AddMemberToClass (project, cls, specificPartToAffect, member, false); // // //some tests in case code refactorer returns bad values // int beginline = specificPartToAffect.BodyRegion.BeginLine; // var region = GetRegion (mem); // if (!region.IsEmpty && region.BeginLine >= beginline && region.BeginLine <= specificPartToAffect.BodyRegion.EndLine) // beginline = region.BeginLine; // // //jump to the member or class // IdeApp.Workbench.OpenDocument (specificPartToAffect.Region.FileName, beginline, 1); // } // // public static System.CodeDom.CodeTypeMember ReflectionToCodeDomMember (MemberInfo memberInfo) // { // if (memberInfo is MethodInfo) // return ReflectionToCodeDomMethod ((MethodInfo) memberInfo); // // throw new NotImplementedException (); // } // // public static System.CodeDom.CodeMemberMethod ReflectionToCodeDomMethod (MethodInfo mi) // { // CodeMemberMethod newMethod = new CodeMemberMethod (); // newMethod.Name = mi.Name; // newMethod.ReturnType = new System.CodeDom.CodeTypeReference (mi.ReturnType.FullName); // // newMethod.Attributes = System.CodeDom.MemberAttributes.Private; // switch (mi.Attributes) { // case System.Reflection.MethodAttributes.Assembly: // newMethod.Attributes |= System.CodeDom.MemberAttributes.Assembly; // break; // case System.Reflection.MethodAttributes.FamANDAssem: // newMethod.Attributes |= System.CodeDom.MemberAttributes.FamilyAndAssembly; // break; // case System.Reflection.MethodAttributes.Family: // newMethod.Attributes |= System.CodeDom.MemberAttributes.Family; // break; // case System.Reflection.MethodAttributes.FamORAssem: // newMethod.Attributes |= System.CodeDom.MemberAttributes.FamilyAndAssembly; // break; // case System.Reflection.MethodAttributes.Public: // newMethod.Attributes |= System.CodeDom.MemberAttributes.Public; // break; // case System.Reflection.MethodAttributes.Static: // newMethod.Attributes |= System.CodeDom.MemberAttributes.Static; // break; // } // // ParameterInfo[] pinfos = mi.GetParameters (); // foreach (ParameterInfo pi in pinfos) { // CodeParameterDeclarationExpression newPar = new CodeParameterDeclarationExpression (pi.ParameterType.FullName, pi.Name); // if (pi.IsIn) newPar.Direction = FieldDirection.In; // else if (pi.IsOut) newPar.Direction = FieldDirection.Out; // newMethod.Parameters.Add (newPar); // } // // return newMethod; // } public static IMethodSymbol GetMethodSignature(IEventSymbol ev) { if (ev.Type == null) { return(null); } ITypeSymbol cls = ev.Type; if (cls.TypeKind == TypeKind.Unknown) { return(null); } foreach (var m in cls.GetAccessibleMembersInThisAndBaseTypes <IMethodSymbol> (cls)) { if (m.Name == "Invoke") { return(m); } } return(null); }
//TODO: check accessibility public static IEnumerable <IMethodSymbol> GetCompatibleMethodsInClass(ITypeSymbol cls, IMethodSymbol matchMeth) { ITypeSymbol[] pars = new ITypeSymbol[matchMeth.Parameters.Length]; List <ITypeSymbol>[] baseTypes = new List <ITypeSymbol> [matchMeth.Parameters.Length]; for (int i = 0; i < matchMeth.Parameters.Length; i++) { pars[i] = matchMeth.Parameters[i].Type; baseTypes[i] = new List <ITypeSymbol> ((GetBaseTypes(pars[i]))); } var matchMethType = matchMeth.ReturnType; foreach (IMethodSymbol method in cls.GetAccessibleMembersInThisAndBaseTypes <IMethodSymbol> (cls)) { if (method.DeclaredAccessibility == Accessibility.Private || method.Parameters.Length != pars.Length || !method.ReturnType.Equals(matchMethType) || method.DeclaredAccessibility == Accessibility.Internal) { continue; } bool allCompatible = true; //compare each parameter for (int i = 0; i < pars.Length; i++) { var pt = method.Parameters[i].Type; bool parCompatible = pars[i].Equals(pt); if (!parCompatible && !baseTypes[i].Any(t => t.Equals(pt))) { allCompatible = false; break; } } if (allCompatible) { yield return(method); } } }
//TODO: check accessibility public static IEnumerable<IMethodSymbol> GetCompatibleMethodsInClass (ITypeSymbol cls, IMethodSymbol matchMeth) { ITypeSymbol[] pars = new ITypeSymbol[matchMeth.Parameters.Length]; List<ITypeSymbol>[] baseTypes = new List<ITypeSymbol>[matchMeth.Parameters.Length]; for (int i = 0; i < matchMeth.Parameters.Length; i++) { pars[i] = matchMeth.Parameters[i].Type; baseTypes[i] = new List<ITypeSymbol> (pars[i].GetBaseTypes ()); } var matchMethType = matchMeth.ReturnType; foreach (IMethodSymbol method in cls.GetAccessibleMembersInThisAndBaseTypes<IMethodSymbol> (cls)) { if (method.DeclaredAccessibility == Accessibility.Private || method.Parameters.Length != pars.Length || !method.ReturnType.Equals (matchMethType) || method.DeclaredAccessibility == Accessibility.Internal) continue; bool allCompatible = true; //compare each parameter for (int i = 0; i < pars.Length; i++) { var pt = method.Parameters[i].Type; bool parCompatible = pars[i].Equals (pt); if (!parCompatible && !baseTypes[i].Any (t => t.Equals (pt))) { allCompatible = false; break; } } if (allCompatible) yield return method; } }