本文整理汇总了C#中ICSharpCode.NRefactory.CSharp.Resolver.MemberLookup类的典型用法代码示例。如果您正苦于以下问题:C# MemberLookup类的具体用法?C# MemberLookup怎么用?C# MemberLookup使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
MemberLookup类属于ICSharpCode.NRefactory.CSharp.Resolver命名空间,在下文中一共展示了MemberLookup类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetActionsForType
IEnumerable<CodeAction> GetActionsForType(RefactoringContext context, AstNode node)
{
var rr = context.Resolve(node) as UnknownIdentifierResolveResult;
if (rr == null)
return EmptyList<CodeAction>.Instance;
string identifier = rr.Identifier;
int tc = rr.TypeArgumentCount;
string attributeIdentifier = null;
if (node.Parent is Attribute)
attributeIdentifier = identifier + "Attribute";
var lookup = new MemberLookup(null, context.Compilation.MainAssembly);
List<CodeAction> actions = new List<CodeAction>();
foreach (var typeDefinition in context.Compilation.GetAllTypeDefinitions()) {
if ((typeDefinition.Name == identifier || typeDefinition.Name == attributeIdentifier)
&& typeDefinition.TypeParameterCount == tc
&& lookup.IsAccessible(typeDefinition, false))
{
if (typeDefinition.DeclaringTypeDefinition == null) {
actions.Add(NewUsingAction(context, node, typeDefinition.Namespace));
}
actions.Add(ReplaceWithFullTypeNameAction(context, node, typeDefinition));
}
}
return actions;
}
示例2: IsAccessible
bool IsAccessible(MemberLookup lookup, INamespace ns)
{
if (ns.Types.Any (t => lookup.IsAccessible (t, false)))
return true;
foreach (var child in ns.ChildNamespaces)
if (IsAccessible (lookup, child))
return true;
return false;
}
示例3: Parse
CSharpUnresolvedFile Parse(string program)
{
SyntaxTree syntaxTree = SyntaxTree.Parse(program, "test.cs");
CSharpUnresolvedFile unresolvedFile = syntaxTree.ToTypeSystem();
project = project.AddOrUpdateFiles(unresolvedFile);
compilation = project.CreateCompilation();
lookup = new MemberLookup(null, compilation.MainAssembly);
return unresolvedFile;
}
示例4: AddNamespace
public void AddNamespace (MemberLookup lookup, INamespace ns)
{
if (usedNamespaces.Contains (ns.Name))
return;
if (!IsAccessible (lookup, ns)) {
usedNamespaces.Add (ns.Name);
return;
}
usedNamespaces.Add (ns.Name);
result.Add (Factory.CreateNamespaceCompletionData (ns));
}
示例5: ConstructorParameterDataProvider
public ConstructorParameterDataProvider (int startOffset, CSharpCompletionTextEditorExtension ext, IType type) : base (startOffset, ext)
{
this.type = type;
var ctx = ext.CSharpParsedFile.GetTypeResolveContext (ext.Compilation, ext.Document.Editor.Caret.Location) as CSharpTypeResolveContext;
var lookup = new MemberLookup (ctx.CurrentTypeDefinition, ext.Compilation.MainAssembly);
bool isProtectedAllowed = ctx.CurrentTypeDefinition != null && type.GetDefinition () != null ? ctx.CurrentTypeDefinition.IsDerivedFrom (type.GetDefinition ()) : false;
foreach (var method in type.GetConstructors ()) {
Console.WriteLine ("constructor:" + method);
if (!lookup.IsAccessible (method, isProtectedAllowed)) {
Console.WriteLine ("skip !!!");
continue;
}
methods.Add (method);
}
}
示例6: ConstructorParameterDataProvider
public ConstructorParameterDataProvider (int startOffset, CSharpCompletionTextEditorExtension ext, IType type) : base (startOffset, ext)
{
this.type = type;
var ctx = ext.CSharpParsedFile.GetTypeResolveContext (ext.Compilation, ext.Document.Editor.Caret.Location) as CSharpTypeResolveContext;
var lookup = new MemberLookup (ctx.CurrentTypeDefinition, ext.Compilation.MainAssembly);
bool isProtectedAllowed = false;
var typeDefinition = type.GetDefinition ();
if (ctx.CurrentTypeDefinition != null && typeDefinition != null) {
isProtectedAllowed = ctx.CurrentTypeDefinition.IsDerivedFrom (ctx.CurrentTypeDefinition.Compilation.Import (typeDefinition));
}
foreach (var method in type.GetConstructors ()) {
if (!lookup.IsAccessible (method, isProtectedAllowed)) {
continue;
}
if (!method.IsBrowsable ())
continue;
methods.Add (method);
}
methods.Sort ((l, r) => l.GetEditorBrowsableState ().CompareTo (r.GetEditorBrowsableState ()));
}
示例7: ConstructorParameterDataProvider
public ConstructorParameterDataProvider (int startOffset, CSharpCompletionTextEditorExtension ext, IType type, AstNode skipInitializer = null) : base (startOffset, ext)
{
this.type = type;
var ctx = ext.CSharpUnresolvedFile.GetTypeResolveContext (ext.UnresolvedFileCompilation, ext.Editor.CaretLocation) as CSharpTypeResolveContext;
var lookup = new MemberLookup (ctx.CurrentTypeDefinition, ext.Compilation.MainAssembly);
bool isProtectedAllowed = false;
var typeDefinition = type.GetDefinition ();
if (ctx.CurrentTypeDefinition != null && typeDefinition != null) {
isProtectedAllowed = ctx.CurrentTypeDefinition.IsDerivedFrom (ctx.CurrentTypeDefinition.Compilation.Import (typeDefinition));
}
foreach (var method in type.GetConstructors ()) {
if (!lookup.IsAccessible (method, isProtectedAllowed)) {
continue;
}
if (!method.IsBrowsable ())
continue;
if (skipInitializer != null && skipInitializer.Parent.StartLocation == method.Region.Begin)
continue;
methods.Add (method);
}
methods.Sort (MethodComparer);
}
示例8: CreateElementList
public IList<ICompletionItem> CreateElementList(XamlCompletionContext context, bool includeAbstract)
{
if (context.ParseInformation == null)
return EmptyList<ICompletionItem>.Instance;
List<ICompletionItem> result = new List<ICompletionItem>();
AXmlElement last = context.ParentElement;
ITextEditor editor = context.Editor;
compilation = SD.ParserService.GetCompilationForFile(editor.FileName);
IUnresolvedFile file = context.ParseInformation.UnresolvedFile;
foreach (string item in XamlConst.GetAllowedItems(context)) {
result.Add(new XamlCompletionItem(item));
}
IType rt = null;
if (last != null) {
if (string.Equals(last.Prefix, context.XamlNamespacePrefix, StringComparison.OrdinalIgnoreCase)) {
if (string.Equals(last.LocalName, "Members", StringComparison.OrdinalIgnoreCase))
return result;
if (string.Equals(last.LocalName, "Code", StringComparison.OrdinalIgnoreCase))
return result;
}
// If we have an element that is not a property or an incomplete
// definition => interpret element as a type.
XamlResolver resolver = new XamlResolver(compilation);
int dotIndex = last.LocalName.IndexOf(".", StringComparison.Ordinal) + 1;
if (dotIndex < 1 || dotIndex == last.LocalName.Length) {
rt = resolver.ResolveType(last.Namespace, last.LocalName.Trim('.'));
string contentPropertyName = GetContentPropertyName(rt.GetDefinition());
// If the type has a content property specified, use its type for completion.
if (!string.IsNullOrEmpty(contentPropertyName)) {
IProperty p = rt.GetProperties(m => m.Name == contentPropertyName).FirstOrDefault();
if (p != null) {
rt = p.ReturnType;
}
}
} else {
string typeName = last.LocalName.Substring(0, dotIndex - 1);
string memberName = last.LocalName.Substring(dotIndex);
rt = resolver.ResolveType(last.Namespace, typeName);
IMember member = rt.GetMembers(m => m.Name == memberName).FirstOrDefault();
if (member != null) {
rt = member.ReturnType;
}
}
}
bool parentAdded = false;
var utd = file.GetInnermostTypeDefinition(editor.Caret.Location);
ITypeDefinition currentTypeDef = null;
if (utd != null) {
currentTypeDef = utd.Resolve(new SimpleTypeResolveContext(compilation.MainAssembly)).GetDefinition();
}
MemberLookup memberLookup = new MemberLookup(currentTypeDef, compilation.MainAssembly);
IList<ITypeDefinition> possibleTypesInCollection = EmptyList<ITypeDefinition>.Instance;
if (rt != null && Extensions.IsListType(rt)) {
possibleTypesInCollection = rt.GetMethods(m => m.Parameters.Count == 1 && "Add".Equals(m.Name, StringComparison.Ordinal))
.Select(m => m.Parameters[0].Type.GetDefinition())
.Where(t => t != null)
.ToList();
}
var items = GetClassesFromContext(context);
foreach (var ns in items) {
foreach (ITypeDefinition td in ns.Value) {
if (td.Kind != TypeKind.Class && (!includeAbstract || td.Kind != TypeKind.Interface))
continue;
if (td.IsStatic || (!includeAbstract && td.IsAbstract) || td.IsDerivedFrom(KnownTypeCode.Attribute))
continue;
if (td.Kind == TypeKind.Class && !td.GetConstructors().Any(m => memberLookup.IsAccessible(m, false)))
continue;
if (possibleTypesInCollection.Count > 0 && !possibleTypesInCollection.Any(td.IsDerivedFrom))
continue;
string fullName = td.Name;
if (!string.IsNullOrEmpty(ns.Key))
fullName = ns.Key + ":" + fullName;
XamlCompletionItem item = new XamlCompletionItem(fullName, td);
parentAdded = parentAdded || (last != null && item.Text == last.Name);
result.Add(item);
}
}
// TODO reimplement this if it is really necessary.
// if (!parentAdded && last != null && !last.Name.Contains(".")) {
// IClass itemClass = cu.CreateType(last.Namespace, last.LocalName.Trim('.')).GetUnderlyingClass();
// if (itemClass != null)
// result.Add(new XamlCodeCompletionItem(itemClass, last.Prefix));
// }
return result;
}
示例9: Run
protected override void Run ()
{
var doc = IdeApp.Workbench.ActiveDocument;
if (doc == null || doc.FileName == FilePath.Null || doc.ParsedDocument == null)
return;
ITextEditorExtension ext = doc.EditorExtension;
while (ext != null && !(ext is CompletionTextEditorExtension))
ext = ext.Next;
if (ext == null)
return;
var dom = doc.Compilation;
ImportSymbolCache cache = new ImportSymbolCache ();
var lookup = new MemberLookup (null, doc.Compilation.MainAssembly);
List<ImportSymbolCompletionData> typeList = new List<ImportSymbolCompletionData> ();
foreach (var type in dom.GetAllTypeDefinitions ()) {
if (!lookup.IsAccessible (type, false))
continue;
typeList.Add (new ImportSymbolCompletionData (doc, cache, type));
}
typeList.Sort (delegate (ImportSymbolCompletionData left, ImportSymbolCompletionData right) {
return left.Type.Name.CompareTo (right.Type.Name);
});
CompletionDataList completionList = new CompletionDataList ();
completionList.IsSorted = true;
typeList.ForEach (cd => completionList.Add (cd));
((CompletionTextEditorExtension)ext).ShowCompletion (completionList);
}
示例10: SetUp
public override void SetUp()
{
base.SetUp();
lookup = new MemberLookup(null, compilation.MainAssembly);
}
示例11: GetPossibleNamespaces
static IEnumerable<PossibleNamespace> GetPossibleNamespaces (Document doc, AstNode node, ResolveResult resolveResult, DocumentLocation location)
{
var unit = doc.ParsedDocument.GetAst<SyntaxTree> ();
if (unit == null)
yield break;
int tc = GetTypeParameterCount (node);
var attribute = unit.GetNodeAt<ICSharpCode.NRefactory.CSharp.Attribute> (location);
bool isInsideAttributeType = attribute != null && attribute.Type.Contains (location);
var compilations = new List<Tuple<ICompilation, MonoDevelop.Projects.ProjectReference>> ();
compilations.Add (Tuple.Create (doc.Compilation, (MonoDevelop.Projects.ProjectReference)null));
var referencedItems = doc.Project.GetReferencedItems (IdeApp.Workspace.ActiveConfiguration).ToList ();
foreach (var project in doc.Project.ParentSolution.GetAllProjects ()) {
if (project == doc.Project || referencedItems.Contains (project))
continue;
var comp = TypeSystemService.GetCompilation (project);
if (comp == null)
continue;
compilations.Add (Tuple.Create (comp, new MonoDevelop.Projects.ProjectReference (project)));
}
var netProject = doc.Project as DotNetProject;
if (netProject == null)
yield break;
var frameworkLookup = TypeSystemService.GetFrameworkLookup (netProject);
if (resolveResult is UnknownMemberResolveResult) {
var umResult = (UnknownMemberResolveResult)resolveResult;
foreach (var r in frameworkLookup.LookupExtensionMethod (umResult.MemberName)) {
var systemAssembly = netProject.AssemblyContext.GetAssemblyFromFullName (r.FullName, r.Package, netProject.TargetFramework);
if (systemAssembly == null)
continue;
compilations.Add (Tuple.Create (TypeSystemService.GetCompilation (systemAssembly, doc.Compilation), new MonoDevelop.Projects.ProjectReference (systemAssembly)));
}
}
var lookup = new MemberLookup (null, doc.Compilation.MainAssembly);
foreach (var comp in compilations) {
var compilation = comp.Item1;
var requiredReference = comp.Item2;
if (resolveResult is AmbiguousTypeResolveResult) {
var aResult = resolveResult as AmbiguousTypeResolveResult;
var file = doc.ParsedDocument.ParsedFile as CSharpUnresolvedFile;
var scope = file.GetUsingScope (location).Resolve (compilation);
while (scope != null) {
foreach (var u in scope.Usings) {
foreach (var typeDefinition in u.Types) {
if (typeDefinition.Name == aResult.Type.Name &&
typeDefinition.TypeParameterCount == tc &&
lookup.IsAccessible (typeDefinition, false)) {
yield return new PossibleNamespace (typeDefinition.Namespace, true, requiredReference);
}
}
}
scope = scope.Parent;
}
}
if (resolveResult is UnknownIdentifierResolveResult) {
var uiResult = resolveResult as UnknownIdentifierResolveResult;
string possibleAttributeName = isInsideAttributeType ? uiResult.Identifier + "Attribute" : null;
foreach (var typeDefinition in compilation.GetAllTypeDefinitions ()) {
if ((typeDefinition.Name == uiResult.Identifier || typeDefinition.Name == possibleAttributeName) && typeDefinition.TypeParameterCount == tc &&
lookup.IsAccessible (typeDefinition, false)) {
if (typeDefinition.DeclaringTypeDefinition != null) {
var builder = new TypeSystemAstBuilder (new CSharpResolver (doc.Compilation));
yield return new PossibleNamespace (builder.ConvertType (typeDefinition.DeclaringTypeDefinition).ToString (), false, requiredReference);
} else {
yield return new PossibleNamespace (typeDefinition.Namespace, true, requiredReference);
}
}
}
}
if (resolveResult is UnknownMemberResolveResult) {
var umResult = (UnknownMemberResolveResult)resolveResult;
string possibleAttributeName = isInsideAttributeType ? umResult.MemberName + "Attribute" : null;
foreach (var typeDefinition in compilation.GetAllTypeDefinitions ().Where (t => t.HasExtensionMethods)) {
foreach (var method in typeDefinition.Methods.Where (m => m.IsExtensionMethod && (m.Name == umResult.MemberName || m.Name == possibleAttributeName))) {
IType[] inferredTypes;
if (CSharpResolver.IsEligibleExtensionMethod (
compilation.Import (umResult.TargetType),
method,
true,
out inferredTypes
)) {
yield return new PossibleNamespace (typeDefinition.Namespace, true, requiredReference);
goto skipType;
}
}
skipType:
;
}
}
if (resolveResult is ErrorResolveResult) {
var identifier = unit != null ? unit.GetNodeAt<Identifier> (location) : null;
if (identifier != null) {
var uiResult = resolveResult as UnknownIdentifierResolveResult;
//.........这里部分代码省略.........
示例12: CreateCompletionData
IEnumerable<ICompletionData> CreateCompletionData(TextLocation location, ResolveResult resolveResult, AstNode resolvedNode, CSharpResolver state, Func<IType, IType> typePred = null)
{
if (resolveResult == null /* || resolveResult.IsError*/) {
return null;
}
var lookup = new MemberLookup(
ctx.CurrentTypeDefinition,
Compilation.MainAssembly
);
if (resolveResult is NamespaceResolveResult) {
var nr = (NamespaceResolveResult)resolveResult;
var namespaceContents = new CompletionDataWrapper(this);
foreach (var cl in nr.Namespace.Types) {
if (!lookup.IsAccessible(cl, false))
continue;
IType addType = typePred != null ? typePred(cl) : cl;
if (addType != null)
namespaceContents.AddType(addType, false);
}
foreach (var ns in nr.Namespace.ChildNamespaces) {
namespaceContents.AddNamespace(lookup, ns);
}
return namespaceContents.Result;
}
IType type = resolveResult.Type;
if (type.Namespace == "System" && type.Name == "Void")
return null;
if (resolvedNode.Parent is PointerReferenceExpression && (type is PointerType)) {
resolveResult = new OperatorResolveResult(((PointerType)type).ElementType, System.Linq.Expressions.ExpressionType.Extension, resolveResult);
}
//var typeDef = resolveResult.Type.GetDefinition();
var result = new CompletionDataWrapper(this);
bool includeStaticMembers = false;
if (resolveResult is LocalResolveResult) {
if (resolvedNode is IdentifierExpression) {
var mrr = (LocalResolveResult)resolveResult;
includeStaticMembers = mrr.Variable.Name == mrr.Type.Name;
}
}
if (resolveResult is TypeResolveResult && type.Kind == TypeKind.Enum) {
foreach (var field in type.GetFields ()) {
if (!lookup.IsAccessible(field, false))
continue;
result.AddMember(field);
}
return result.Result;
}
bool isProtectedAllowed = resolveResult is ThisResolveResult ? true : lookup.IsProtectedAccessAllowed(type);
bool skipNonStaticMembers = (resolveResult is TypeResolveResult);
if (resolveResult is MemberResolveResult && resolvedNode is IdentifierExpression) {
var mrr = (MemberResolveResult)resolveResult;
includeStaticMembers = mrr.Member.Name == mrr.Type.Name;
TypeResolveResult trr;
if (state.IsVariableReferenceWithSameType(
resolveResult,
((IdentifierExpression)resolvedNode).Identifier,
out trr
)) {
if (currentMember != null && mrr.Member.IsStatic ^ currentMember.IsStatic) {
skipNonStaticMembers = true;
if (trr.Type.Kind == TypeKind.Enum) {
foreach (var field in trr.Type.GetFields ()) {
if (lookup.IsAccessible(field, false))
result.AddMember(field);
}
return result.Result;
}
}
}
// ADD Aliases
var scope = ctx.CurrentUsingScope;
for (var n = scope; n != null; n = n.Parent) {
foreach (var pair in n.UsingAliases) {
if (pair.Key == mrr.Member.Name) {
foreach (var r in CreateCompletionData (location, pair.Value, resolvedNode, state)) {
if (r is IEntityCompletionData && ((IEntityCompletionData)r).Entity is IMember) {
result.AddMember((IMember)((IEntityCompletionData)r).Entity);
} else {
result.Add(r);
}
}
}
}
}
}
//.........这里部分代码省略.........
示例13: GetImportCompletionData
/// <summary>
/// Gets the types that needs to be imported via using or full type name.
/// </summary>
public IEnumerable<ICompletionData> GetImportCompletionData(int offset)
{
var generalLookup = new MemberLookup(null, Compilation.MainAssembly);
SetOffset(offset);
// flatten usings
var namespaces = new List<INamespace>();
for (var n = ctx.CurrentUsingScope; n != null; n = n.Parent) {
namespaces.Add(n.Namespace);
foreach (var u in n.Usings)
namespaces.Add(u);
}
foreach (var type in Compilation.GetAllTypeDefinitions ()) {
if (!generalLookup.IsAccessible(type, false))
continue;
if (namespaces.Any(n => n.FullName == type.Namespace))
continue;
bool useFullName = false;
foreach (var ns in namespaces) {
if (ns.GetTypeDefinition(type.Name, type.TypeParameterCount) != null) {
useFullName = true;
break;
}
}
yield return factory.CreateImportCompletionData(type, useFullName, false);
}
}
示例14: AddTypesAndNamespaces
void AddTypesAndNamespaces(CompletionDataWrapper wrapper, CSharpResolver state, AstNode node, Func<IType, IType> typePred = null, Predicate<IMember> memberPred = null, Action<ICompletionData, IType> callback = null)
{
var lookup = new MemberLookup(
ctx.CurrentTypeDefinition,
Compilation.MainAssembly
);
if (currentType != null) {
for (var ct = currentType; ct != null; ct = ct.DeclaringTypeDefinition) {
foreach (var nestedType in ct.NestedTypes) {
string name = nestedType.Name;
if (IsAttributeContext(node) && name.EndsWith("Attribute") && name.Length > "Attribute".Length) {
name = name.Substring(0, name.Length - "Attribute".Length);
}
if (typePred == null) {
wrapper.AddType(nestedType, name);
continue;
}
var type = typePred(nestedType.Resolve(ctx));
if (type != null) {
var a2 = wrapper.AddType(type, name);
if (a2 != null && callback != null) {
callback(a2, type);
}
}
continue;
}
}
if (this.currentMember != null && !(node is AstType)) {
var def = ctx.CurrentTypeDefinition ?? Compilation.MainAssembly.GetTypeDefinition(currentType);
if (def != null) {
bool isProtectedAllowed = true;
foreach (var member in def.GetMembers ()) {
if (member is IMethod && ((IMethod)member).FullName == "System.Object.Finalize") {
continue;
}
if (member.EntityType == EntityType.Operator) {
continue;
}
if (member.IsExplicitInterfaceImplementation) {
continue;
}
if (!lookup.IsAccessible(member, isProtectedAllowed)) {
continue;
}
if (memberPred == null || memberPred(member)) {
wrapper.AddMember(member);
}
}
var declaring = def.DeclaringTypeDefinition;
while (declaring != null) {
foreach (var member in declaring.GetMembers (m => m.IsStatic)) {
if (memberPred == null || memberPred(member)) {
wrapper.AddMember(member);
}
}
declaring = declaring.DeclaringTypeDefinition;
}
}
}
foreach (var p in currentType.TypeParameters) {
wrapper.AddTypeParameter(p);
}
}
var scope = CSharpParsedFile.GetUsingScope(location).Resolve(Compilation);
for (var n = scope; n != null; n = n.Parent) {
foreach (var pair in n.UsingAliases) {
wrapper.AddNamespace(pair.Key);
}
foreach (var u in n.Usings) {
foreach (var type in u.Types) {
if (!lookup.IsAccessible(type, false))
continue;
IType addType = typePred != null ? typePred(type) : type;
if (addType != null) {
string name = type.Name;
if (IsAttributeContext(node) && name.EndsWith("Attribute") && name.Length > "Attribute".Length) {
name = name.Substring(0, name.Length - "Attribute".Length);
}
var a = wrapper.AddType(addType, name);
if (a != null && callback != null) {
callback(a, type);
}
}
}
}
foreach (var type in n.Namespace.Types) {
if (!lookup.IsAccessible(type, false))
continue;
IType addType = typePred != null ? typePred(type) : type;
if (addType != null) {
var a2 = wrapper.AddType(addType, addType.Name);
if (a2 != null && callback != null) {
callback(a2, type);
}
//.........这里部分代码省略.........
示例15: GetAccessibleIndexers
IEnumerable<IProperty> GetAccessibleIndexers(IType type)
{
var lookup = new MemberLookup(ctx.CurrentTypeDefinition, Compilation.MainAssembly);
var properties = new List<IProperty>();
foreach (var property in type.GetProperties ()) {
if (!property.IsIndexer)
continue;
if (!lookup.IsAccessible (property, true))
continue;
if (property.IsShadowing) {
for (int j = 0; j < properties.Count; j++) {
if (ParameterListComparer.Instance.Equals(properties[j].Parameters, property.Parameters)) {
properties.RemoveAt (j);
j--;
}
}
}
properties.Add (property);
}
return properties;
}