提交 2449bcde 编写于 作者: C Charles Stoner

Allow extension methods in script code

上级 93d3ced6
......@@ -146,6 +146,17 @@ internal override bool SupportsExtensionMethods
{
((NamespaceSymbol)_container).GetExtensionMethods(methods, name, arity, options);
}
else if (((NamedTypeSymbol)_container).IsScriptClass)
{
for (var submission = this.Compilation; submission != null; submission = submission.PreviousSubmission)
{
var scriptClass = submission.ScriptClass;
if ((object)scriptClass != null)
{
scriptClass.GetExtensionMethods(methods, name, arity, options);
}
}
}
}
}
......
......@@ -3151,7 +3151,7 @@ internal bool ContainsExtensionMethods
{
if (!_lazyContainsExtensionMethods.HasValue())
{
bool containsExtensionMethods = (this.IsStatic && !this.IsGenericType && this.declaration.ContainsExtensionMethods);
bool containsExtensionMethods = ((this.IsStatic && !this.IsGenericType) || this.IsScriptClass) && this.declaration.ContainsExtensionMethods;
_lazyContainsExtensionMethods = containsExtensionMethods.ToThreeState();
}
......
......@@ -221,7 +221,7 @@ private void MethodChecks(MethodDeclarationSyntax syntax, Binder withTypeParamsB
{
diagnostics.Add(ErrorCode.ERR_ExtensionMethodsDecl, location, ContainingType.Name);
}
else if (!ContainingType.IsStatic || ContainingType.Arity != 0)
else if (!ContainingType.IsScriptClass && !(ContainingType.IsStatic && ContainingType.Arity == 0))
{
// Duplicate Dev10 behavior by selecting the containing type identifier. However if there
// is no containing type (in the interactive case for instance), select the method identifier.
......
......@@ -3671,5 +3671,46 @@ static void Main(string[] args)
Assert.Contains("GetEnumerableDisposable2", symbols);
Assert.Contains("GetEnumerableDisposable1", symbols);
}
[Fact]
public void ScriptExtensionMethods()
{
var source =
@"static object F(this object o) { return null; }
class C
{
void M() { this.F(); }
}
var o = new object();
o.F();";
var compilation = CreateCompilationWithMscorlib(source, references: new[] { SystemCoreRef }, parseOptions: TestOptions.Script);
compilation.VerifyDiagnostics();
}
[Fact]
public void InteractiveExtensionMethods()
{
var parseOptions = TestOptions.Interactive;
var references = new[] { MscorlibRef, SystemCoreRef };
var source0 =
@"static object F(this object o) { return 0; }
var o = new object();
o.F();";
var source1 =
@"static object G(this object o) { return 1; }
var o = new object();
o.G().F();";
var s0 = CSharpCompilation.CreateSubmission(
"s0.dll",
syntaxTree: SyntaxFactory.ParseSyntaxTree(source0, options: parseOptions),
references: references);
s0.VerifyDiagnostics();
var s1 = CSharpCompilation.CreateSubmission(
"s1.dll",
syntaxTree: SyntaxFactory.ParseSyntaxTree(source1, options: parseOptions),
previousSubmission: s0,
references: references);
s1.VerifyDiagnostics();
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册