Giter Site home page Giter Site logo

Comments (8)

amcasey avatar amcasey commented on May 23, 2024

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.

JoshVarty avatar JoshVarty commented on May 23, 2024

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.

amcasey avatar amcasey commented on May 23, 2024

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.

JoshVarty avatar JoshVarty commented on May 23, 2024

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.

amcasey avatar amcasey commented on May 23, 2024

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.

mkrueger avatar mkrueger commented on May 23, 2024

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.

 avatar commented on May 23, 2024

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.

ceztko avatar ceztko commented on May 23, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.