Skip to content

Commit

Permalink
Merge branch 'private-extension-methods'
Browse files Browse the repository at this point in the history
  • Loading branch information
erik-kallen committed Oct 12, 2013
2 parents b214f23 + 6b32a28 commit 2aa17da
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
3 changes: 2 additions & 1 deletion ICSharpCode.NRefactory.CSharp/Resolver/CSharpResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1833,11 +1833,12 @@ public List<List<IMethod>> GetExtensionMethods(string name = null, IList<IType>
/// </remarks>
public List<List<IMethod>> GetExtensionMethods(IType targetType, string name = null, IList<IType> typeArguments = null, bool substituteInferredTypes = false)
{
var lookup = CreateMemberLookup();
List<List<IMethod>> extensionMethodGroups = new List<List<IMethod>>();
foreach (var inputGroup in GetAllExtensionMethods()) {
List<IMethod> outputGroup = new List<IMethod>();
foreach (var method in inputGroup) {
if (name != null && method.Name != name)
if ((name != null && method.Name != name) || !lookup.IsAccessible(method, false))
continue;

IType[] inferredTypes;
Expand Down
48 changes: 48 additions & 0 deletions ICSharpCode.NRefactory.Tests/CSharp/Resolver/InvocationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -958,5 +958,53 @@ public static void M() {
Assert.That(rr.Member, Is.InstanceOf<SpecializedMember>());
Assert.That(rr.Member.Parameters[0].Type.Kind == TypeKind.Array);
}

[Test]
public void PrivateExtensionMethodIsNotUsableFromOtherClass() {
string program = @"
using System.Collections.Generic;
namespace Foo {
public static class FooExtensions {
static T Extension<T>(this object value) { return default(T); }
}
}
namespace Bar {
public static class BarExtensions {
public static IEnumerable<T> Extension<T>(this object value) { return new T[0]; }
}
}
namespace Bazz {
using Foo;
using Bar;
public class Client {
public void Method() {
var x = $new object().Extension<int>()$;
}
}
}";
var rr = Resolve<CSharpInvocationResolveResult>(program);
Assert.IsFalse(rr.IsError);
Assert.AreEqual(rr.Type.FullName, "System.Collections.Generic.IEnumerable");
}

[Test]
public void PrivateExtensionMethodIsUsableFromSameClass() {
string program = @"
using System.Collections.Generic;
namespace Foo {
public static class FooExtensions {
static T Extension<T>(this object value) { return default(T); }
static void Method() {
var x = $new object().Extension<int>()$;
}
}
}";
var rr = Resolve<CSharpInvocationResolveResult>(program);
Assert.IsFalse(rr.IsError);
Assert.AreEqual(rr.Type.FullName, "System.Int32");
}
}
}

0 comments on commit 2aa17da

Please sign in to comment.