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)
- Suppressing one IDExxxx rule from a multi-rule EditorConfig setting is not possible if EnforceCodeStyleInBuild is enabled HOT 15
- Syntax coloring broken after updating to 17.10 HOT 5
- Visual Studio won't show errors and Error list doesn't switch to code line after doubl click HOT 13
- [Automated] PRs inserted in VS build main-35005.113
- [Automated] PRs inserted in VS build feature.debugger.inlineValues-35005.76
- Confusing doc comments and method names in implementation of VB's PENamespaceSymbol
- Ref safety analysis does not properly handle readonly set methods
- IDE0028 catches `ICollection<KeyValuePair<TKey, TValue>> = new Dictionary<TKey, TValue>()` as being reduceable HOT 15
- There is either a test gap, or an unnecessary attempt to lookup a local in a map in ClosureConversion
- [Automated] PRs inserted in VS build feature.debugger.main-35006.210
- Edit.Duplicate command does not insert trailing newline between original and duplicated code HOT 7
- `Remove unused using` showing up multiple times in lightbulb HOT 1
- Parameter names in target-typed new() expressions are missing among completions in compound assignment statements HOT 2
- Remove usage of IGlobalOptionService from test helpers on Features layer and below
- Proposal: Flow attributes on primary constructor parameters to generated fields HOT 8
- Please give users a choice to disable implicit usings and nullable before code is generated HOT 3
- [Automated] PRs inserted in VS build main-35007.86
- [Automated] PRs inserted in VS build main-35007.84
- Assert failure in `BindMethodBody.assertBindIdentifierTargets()` with `[param:]` attribute on local function
- Semantic snippets test flaw HOT 2
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.