Comments (8)
Doesn't the fact that PartialImplementationPart is null tell you that it has no body? I believe PartialDefinitionPart is null because you're already on the definition part.
/// <summary>
/// If this is a partial method implementation part, returns the corresponding
/// definition part. Otherwise null.
/// </summary>
IMethodSymbol PartialDefinitionPart { get; }
from roslyn.
I think the issue here is that it's difficult to figure out if it's a partial method if you have the IMethodSymbol
. Both PartialDefinitionPart
and PartialImplementationPart
are null.
For example:
var tree = CSharpSyntaxTree.ParseText(@"
public class MyClass
{
partial void MyMethod();
}");
var Mscorlib = PortableExecutableReference.CreateFromAssembly(typeof(object).Assembly);
var compilation = CSharpCompilation.Create("MyCompilation",
syntaxTrees: new[] { tree }, references: new[] { Mscorlib });
var model = compilation.GetSemanticModel(tree);
var methodSyntax = tree.GetRoot().DescendantNodes().OfType<MethodDeclarationSyntax>().Single();
var methodSymbol = model.GetDeclaredSymbol(methodSyntax);
//Both are null. The same results for non-partial methods.
Console.WriteLine(methodSymbol.PartialDefinitionPart);
Console.WriteLine(methodSymbol.PartialImplementationPart);
from roslyn.
Well that seems unhelpful. @rchande tells me that the IDE iterates over the DeclaringSyntaxNodes property but that doesn't seem like a very clean solution. There should probably be an API for this.
from roslyn.
I'd propose the addition of an IsPartial
property on IMethodSymbol
. I think it would solve @mkrueger's problem and make partials easier to detect. It also seems to fit well with the rest of the API as there is an easy way to detect most other modifiers via IsPublic
, IsStatic
, IsPartial
, IsVirtual
, IsAsync
and so on.
Perhaps the broader question is "Should there be an Is_____ field for all modifiers?" I believe IsUnsafe
is missing as well.
from roslyn.
I suspect the reason that it doesn't exist now is that - unlike public, static, and virtual - partial is a source-only concept. Since we have exposed other aspects of partial-ness on IMethodSymbol, we should probably expose IsPartial as well. However, I doubt we will end up exposing IsUnsafe - it's a source concept that is easily checked on the DeclaringSyntaxNodes.
from roslyn.
Using the nodes is fine for me.
I use now this method that:
static bool IsEmptyPartialMethod(IMethodSymbol method, CancellationToken cancellationToken = default(CancellationToken))
{
if (method.IsDefinedInMetadata ())
return false;
foreach (var r in method.DeclaringSyntaxReferences) {
var node = r.GetSyntax (cancellationToken) as MethodDeclarationSyntax;
if (node == null)
continue;
if (node.Body != null || !node.Modifiers.Any(m => m.IsKind (SyntaxKind.PartialKeyword)))
return false;
}
return true;
}
I think that issue can be closed.
from roslyn.
Getting the syntax node, then checking the modifiers and the existence of the body works, but there should be an out-of-the-box better solution for this. Going from the symbol to the syntax is time consuming. This information could simply be stored in the IMethodSymbol
.
Is there any plan to add a flag for this?
from roslyn.
Getting the syntax node, then checking the modifiers and the existence of the body works, but there should be an out-of-the-box better solution for this. Going from the symbol to the syntax is time consuming. This information could simply be stored in the
IMethodSymbol
.Is there any plan to add a flag for this?
I definetly agree with @ghost: this information should be present in IMethodSymbol
. Also, I found @mkrueger method not working in my case, as I found DeclaringSyntaxReferences
to have only one item even when both declaration and implementation were present. Here is my fixed version:
public static bool IsPartialMethod(this IMethodSymbol method, out bool hasEmptyBody)
{
if (method.IsDefinedInMetadata())
{
hasEmptyBody = false;
return false;
}
foreach (var reference in method.DeclaringSyntaxReferences)
{
var syntax = reference.GetSyntax();
if (syntax.Kind() != SyntaxKind.MethodDeclaration)
continue;
var node = syntax as MethodDeclarationSyntax;
if (!node.Modifiers.Any(SyntaxKind.PartialKeyword))
{
hasEmptyBody = false;
return false;
}
}
hasEmptyBody = method.PartialImplementationPart == null || method.PartialDefinitionPart != null;
return true;
}
/// <returns>False if it's not defined in source</returns>
public static bool IsDefinedInMetadata(this ISymbol symbol)
{
return symbol.Locations.Any(loc => loc.IsInMetadata);
}
from roslyn.
Related Issues (20)
- The "Extract method" refactoring should recognize primary constructor parameters
- Followup work to consider after removing ForegroundThreadAffinitizedObject
- Add SymbolDisplayGenericsOptions.IncludeArity as an alternative to SymbolDisplayGenericsOptions.IncludeTypeParameters HOT 14
- Unexpected SemanticModel responses related to ValueTuples when it involves a nullable element
- Expose Roslyn protocol types as binary dependency to Xaml, Razor
- Source Generation Issue HOT 17
- CS8998 when last character is double quote in raw string literal HOT 2
- Cutting/Pasting a selected Region fails to persist it's collapsed state
- `KeyNotFoundException` during compilation of `goto` that leaves lambda
- IDE0053 has a non-deterministic title in SARIF logs HOT 3
- Error when performing namespace adjust for solution or sync namespace context menu HOT 1
- Compiler task is passing `/generatedfilesout` even though it didn't create the directory HOT 9
- [Automated] PRs inserted in VS build main-34817.155
- Issue with Strong Name Signing stopping IntelliSense from working
- The caracteristic "Missing Importations detection" is not available due to an internal error. HOT 4
- Floating-point to integer conversions should saturate on overflow HOT 1
- Test CollectionBuilder_MissingInt32 hits an assertion failure when lowering layer does not use int32
- Test Plan for "Partial Properties" feature
- CS1503 cannot convert from 'object' to HOT 1
- 'CanCloseSaveDialog' is flakey. HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from roslyn.