Пример #1
0
        public void Recursion()
        {
            var code          = @"
namespace RoslynSandbox
{
    using System;

    class C
    {
        public C()
        {
            Type type;
            type = type;
            var methodInfo = type.GetMethod(nameof(this.ToString));
        }
    }
}";
            var syntaxTree    = CSharpSyntaxTree.ParseText(code);
            var compilation   = CSharpCompilation.Create("test", new[] { syntaxTree }, MetadataReferences.FromAttributes());
            var semanticModel = compilation.GetSemanticModel(syntaxTree);
            var node          = syntaxTree.FindInvocation("GetMethod");
            var context       = new SyntaxNodeAnalysisContext(null, null, semanticModel, null, null, null, CancellationToken.None);

            Assert.AreEqual(false, ReflectedMember.TryGetType(node, context, out _, out _));
        }
        public static void TryGetTypeFromExpression(string call, string expected, string expectedSource)
        {
            var code          = @"
namespace N
{
    using System.Collections.Generic;
    using System.Reflection;

    class C
    {
        public C(C foo)
        {
            var methodInfo = typeof(C).GetMethod(nameof(this.ToString));
        }
    }
}".AssertReplace("typeof(C).GetMethod(nameof(this.ToString))", call);
            var syntaxTree    = CSharpSyntaxTree.ParseText(code);
            var compilation   = CSharpCompilation.Create("test", new[] { syntaxTree }, MetadataReferences.FromAttributes());
            var semanticModel = compilation.GetSemanticModel(syntaxTree);
            var node          = syntaxTree.FindInvocation(call);

            Assert.AreEqual(true, ReflectedMember.TryGetType(node, semanticModel, CancellationToken.None, out var type, out var source));
            Assert.AreEqual(expected, type.MetadataName);
            Assert.AreEqual(expectedSource, source.ToString());
        }
Пример #3
0
        public void TryGetTypeFromLocal(string typeExpression, string expected)
        {
            var code          = @"
namespace RoslynSandbox
{
    using System.Collections.Generic;
    using System.Reflection;

    class C
    {
        public C(C foo)
        {
            var type = typeof(C);
            var methodInfo = type.GetMethod(nameof(this.ToString));
        }
    }
}".AssertReplace("typeof(C)", typeExpression);
            var syntaxTree    = CSharpSyntaxTree.ParseText(code);
            var compilation   = CSharpCompilation.Create("test", new[] { syntaxTree }, MetadataReferences.FromAttributes());
            var semanticModel = compilation.GetSemanticModel(syntaxTree);
            var node          = syntaxTree.FindInvocation("GetMethod");
            var context       = new SyntaxNodeAnalysisContext(null, null, semanticModel, null, null, null, CancellationToken.None);

            Assert.AreEqual(true, ReflectedMember.TryGetType(node, context, out var type, out var instance));
            Assert.AreEqual(expected, type.MetadataName);
            Assert.AreEqual(typeExpression, instance.ToString());
        }
        public static void Recursion()
        {
            var code          = @"
namespace N
{
    using System;

    class C
    {
        public C()
        {
            Type type;
            type = type;
            var methodInfo = type.GetMethod(nameof(this.ToString));
        }
    }
}";
            var syntaxTree    = CSharpSyntaxTree.ParseText(code);
            var compilation   = CSharpCompilation.Create("test", new[] { syntaxTree }, Settings.Default.MetadataReferences);
            var semanticModel = compilation.GetSemanticModel(syntaxTree);
            var node          = syntaxTree.FindInvocation("GetMethod");

            Assert.AreEqual(false, ReflectedMember.TryGetType(node, semanticModel, CancellationToken.None, out _, out _));
        }