Looks class that is the method container using Roslyn. - CSharp System.Reflection

CSharp examples for System.Reflection:MethodInfo

Description

Looks class that is the method container using Roslyn.

Demo Code


using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis;

public class Main{
        /// <summary>
        /// Looks class that is the method container.
        /// </summary>
        /// <param name="methodSyntax"></param>
        /// <returns></returns>
        public static ClassDeclarationSyntax DeclaringClass(this MethodDeclarationSyntax methodSyntax)
        {//from  ww  w  .  j a va2 s  . co m
            if (methodSyntax.Parent is ClassDeclarationSyntax)
                return methodSyntax.Parent as ClassDeclarationSyntax;

            return null;
        }
}

Related Tutorials