コード例 #1
0
        public static void Equal()
        {
            var syntaxTree = CSharpSyntaxTree.ParseText(
                @"
namespace N
{
    public class C
    {
        public int M1() => 1;

        public int M2() => 2;
    }
}");
            var compilation   = CSharpCompilation.Create("test", new[] { syntaxTree });
            var semanticModel = compilation.GetSemanticModel(syntaxTree);
            var node1         = syntaxTree.FindMethodDeclaration("M1");
            var symbol1       = semanticModel.GetDeclaredSymbol(node1, CancellationToken.None);
            var node2         = syntaxTree.FindMethodDeclaration("M2");
            var symbol2       = semanticModel.GetDeclaredSymbol(node2, CancellationToken.None);

            Assert.AreEqual(true, SymbolComparer.Equal((ISymbol)symbol1, (ISymbol)symbol1));
            Assert.AreEqual(false, SymbolComparer.Equal((ISymbol)symbol1, (ISymbol)symbol2));
            Assert.AreEqual(true, MethodSymbolComparer.Equal(symbol1, symbol1));
            Assert.AreEqual(false, MethodSymbolComparer.Equal(symbol1, symbol2));
            Assert.AreEqual(SymbolComparer.Default.GetHashCode(symbol1), MethodSymbolComparer.Default.GetHashCode(symbol1));
            Assert.AreNotEqual(SymbolComparer.Default.GetHashCode(symbol1), MethodSymbolComparer.Default.GetHashCode(symbol2));
        }
コード例 #2
0
        public static void Equals()
        {
            var syntaxTree = CSharpSyntaxTree.ParseText(
                @"
namespace N
{
    public class C
    {
        public C()
        {
            var bar1;
            var bar2;
        }
    }
}");
            var compilation   = CSharpCompilation.Create("test", new[] { syntaxTree });
            var semanticModel = compilation.GetSemanticModel(syntaxTree);
            var node1         = syntaxTree.FindVariableDeclaration("bar1");
            var symbol1       = semanticModel.GetDeclaredSymbol(node1.Variables[0], CancellationToken.None);
            var node2         = syntaxTree.FindVariableDeclaration("bar2");
            var symbol2       = semanticModel.GetDeclaredSymbol(node2.Variables[0], CancellationToken.None);

            Assert.AreEqual(true, SymbolComparer.Equal(symbol1, symbol1));
            Assert.AreEqual(false, SymbolComparer.Equal(symbol1, symbol2));
            Assert.AreEqual(true, LocalSymbolComparer.Equal((ILocalSymbol)symbol1, (ILocalSymbol)symbol1));
            Assert.AreEqual(false, LocalSymbolComparer.Equal((ILocalSymbol)symbol1, (ILocalSymbol)symbol2));
            Assert.AreEqual(SymbolComparer.Default.GetHashCode(symbol1), LocalSymbolComparer.Default.GetHashCode((ILocalSymbol)symbol1));
            Assert.AreNotEqual(SymbolComparer.Default.GetHashCode(symbol1), LocalSymbolComparer.Default.GetHashCode((ILocalSymbol)symbol2));
        }
コード例 #3
0
        public static void Equal()
        {
            var syntaxTree = CSharpSyntaxTree.ParseText(
                @"
namespace N
{
    using System;

    public class C
    {
        public event EventHandler E1;
        public event EventHandler E2;
    }
}");
            var compilation   = CSharpCompilation.Create("test", new[] { syntaxTree });
            var semanticModel = compilation.GetSemanticModel(syntaxTree);
            var node1         = syntaxTree.FindEventFieldDeclaration("E1");
            var symbol1       = semanticModel.GetDeclaredSymbol(node1.Declaration.Variables[0], CancellationToken.None);
            var node2         = syntaxTree.FindEventFieldDeclaration("E2");
            var symbol2       = semanticModel.GetDeclaredSymbol(node2.Declaration.Variables[0], CancellationToken.None);

            Assert.AreEqual(true, SymbolComparer.Equal(symbol1, symbol1));
            Assert.AreEqual(false, SymbolComparer.Equal(symbol1, symbol2));
            Assert.AreEqual(true, EventSymbolComparer.Equal((IEventSymbol)symbol1, (IEventSymbol)symbol1));
            Assert.AreEqual(false, EventSymbolComparer.Equal((IEventSymbol)symbol1, (IEventSymbol)symbol2));
            Assert.AreEqual(SymbolComparer.Default.GetHashCode(symbol1), EventSymbolComparer.Default.GetHashCode((IEventSymbol)symbol1));
            Assert.AreNotEqual(SymbolComparer.Default.GetHashCode(symbol1), EventSymbolComparer.Default.GetHashCode((IEventSymbol)symbol2));
        }
コード例 #4
0
        public static void GenericMethodsArgumentTypeParameterEquals()
        {
            var syntaxTree = CSharpSyntaxTree.ParseText(
                @"
namespace N
{
    public class C<T>
    {
        public int M1(T x) => 1;

        public int M2(T x) => 2;
    }
}");
            var compilation   = CSharpCompilation.Create("test", new[] { syntaxTree });
            var semanticModel = compilation.GetSemanticModel(syntaxTree);
            var node1         = syntaxTree.FindMethodDeclaration("M1");
            var symbol1       = semanticModel.GetDeclaredSymbol(node1, CancellationToken.None).Parameters[0].Type;
            var node2         = syntaxTree.FindMethodDeclaration("M2");
            var symbol2       = semanticModel.GetDeclaredSymbol(node2, CancellationToken.None).Parameters[0].Type;

            Assert.AreEqual(true, SymbolComparer.Equal((ISymbol)symbol1, (ISymbol)symbol1));
            Assert.AreEqual(true, SymbolComparer.Equal((ISymbol)symbol1, (ISymbol)symbol2));
            Assert.AreEqual(true, TypeSymbolComparer.Equal((ITypeSymbol)symbol1, (ITypeSymbol)symbol1));
            Assert.AreEqual(true, TypeSymbolComparer.Equal((ITypeSymbol)symbol1, (ITypeSymbol)symbol2));
            Assert.AreEqual(SymbolComparer.Default.GetHashCode(symbol1), TypeSymbolComparer.GetHashCode(symbol1));
            Assert.AreEqual(SymbolComparer.Default.GetHashCode(symbol1), TypeSymbolComparer.GetHashCode(symbol2));
        }
コード例 #5
0
        public static void GenericPropertiesTypeParameterEquals()
        {
            var syntaxTree = CSharpSyntaxTree.ParseText(
                @"
namespace N
{
    public class C<T>
    {
        public T P1 { get; }
        public T P2 { get; }
    }
}");
            var compilation   = CSharpCompilation.Create("test", new[] { syntaxTree });
            var semanticModel = compilation.GetSemanticModel(syntaxTree);
            var node1         = syntaxTree.FindPropertyDeclaration("P1");
            var symbol1       = semanticModel.GetDeclaredSymbol(node1, CancellationToken.None).Type;
            var node2         = syntaxTree.FindPropertyDeclaration("P2");
            var symbol2       = semanticModel.GetDeclaredSymbol(node2, CancellationToken.None).Type;

            Assert.AreEqual(true, SymbolComparer.Equal((ISymbol)symbol1, (ISymbol)symbol1));
            Assert.AreEqual(true, SymbolComparer.Equal((ISymbol)symbol1, (ISymbol)symbol2));
            Assert.AreEqual(true, TypeSymbolComparer.Equal((ITypeSymbol)symbol1, (ITypeSymbol)symbol1));
            Assert.AreEqual(true, TypeSymbolComparer.Equal((ITypeSymbol)symbol1, (ITypeSymbol)symbol2));
            Assert.AreEqual(SymbolComparer.Default.GetHashCode(symbol1), TypeSymbolComparer.GetHashCode(symbol1));
            Assert.AreEqual(SymbolComparer.Default.GetHashCode(symbol1), TypeSymbolComparer.GetHashCode(symbol2));
        }
コード例 #6
0
        public static void Equals()
        {
            var syntaxTree = CSharpSyntaxTree.ParseText(
                @"
namespace N
{
    public class C1
    {
    }

    public class C2
    {
    }
}");
            var compilation   = CSharpCompilation.Create("test", new[] { syntaxTree });
            var semanticModel = compilation.GetSemanticModel(syntaxTree);
            var node1         = syntaxTree.FindTypeDeclaration("C1");
            var symbol1       = semanticModel.GetDeclaredSymbol(node1, CancellationToken.None);
            var node2         = syntaxTree.FindTypeDeclaration("C2");
            var symbol2       = semanticModel.GetDeclaredSymbol(node2, CancellationToken.None);

            Assert.AreEqual(true, SymbolComparer.Equal((ISymbol)symbol1, (ISymbol)symbol1));
            Assert.AreEqual(false, SymbolComparer.Equal((ISymbol)symbol1, (ISymbol)symbol2));
            Assert.AreEqual(true, TypeSymbolComparer.Equal((ITypeSymbol)symbol1, (ITypeSymbol)symbol1));
            Assert.AreEqual(false, TypeSymbolComparer.Equal((ITypeSymbol)symbol1, (ITypeSymbol)symbol2));
            Assert.AreEqual(SymbolComparer.Default.GetHashCode(symbol1), TypeSymbolComparer.GetHashCode(symbol1));
            Assert.AreNotEqual(SymbolComparer.Default.GetHashCode(symbol1), TypeSymbolComparer.GetHashCode(symbol2));
        }
コード例 #7
0
        public static void Inherited()
        {
            var syntaxTree = CSharpSyntaxTree.ParseText(
                @"
namespace N
{
    public class C1
    {
        protected int F;
    }

    public class C2 : C1
    {
        public C2()
        {
            var temp = this.F;
        }
    }
}");
            var compilation   = CSharpCompilation.Create("test", new[] { syntaxTree });
            var semanticModel = compilation.GetSemanticModel(syntaxTree);
            var node1         = syntaxTree.FindFieldDeclaration("F");
            var symbol1       = semanticModel.GetDeclaredSymbol(node1.Declaration.Variables[0], CancellationToken.None);
            var node2         = syntaxTree.FindMemberAccessExpression("this.F");
            var symbol2       = semanticModel.GetSymbolInfo(node2, CancellationToken.None).Symbol;

            Assert.AreEqual(true, SymbolComparer.Equal(symbol1, symbol1));
            Assert.AreEqual(true, SymbolComparer.Equal(symbol1, symbol2));
            Assert.AreEqual(true, FieldSymbolComparer.Equal((IFieldSymbol)symbol1, (IFieldSymbol)symbol1));
            Assert.AreEqual(true, FieldSymbolComparer.Equal((IFieldSymbol)symbol1, (IFieldSymbol)symbol2));
            Assert.AreEqual(SymbolComparer.Default.GetHashCode(symbol1), FieldSymbolComparer.Default.GetHashCode((IFieldSymbol)symbol1));
            Assert.AreEqual(SymbolComparer.Default.GetHashCode(symbol1), FieldSymbolComparer.Default.GetHashCode((IFieldSymbol)symbol2));
        }
コード例 #8
0
        public static void TwoGenericOut()
        {
            var syntaxTree = CSharpSyntaxTree.ParseText(
                @"
namespace N
{
    class C
    {
        int M<T>(out T t1, out T t2)
        {
            t1 = default;
            t2 = default;
            return 1;
        }
    }
}");
            var compilation   = CSharpCompilation.Create("test", new[] { syntaxTree });
            var semanticModel = compilation.GetSemanticModel(syntaxTree);
            var parameters    = syntaxTree.FindMethodDeclaration("M").ParameterList.Parameters;
            var symbol1       = semanticModel.GetDeclaredSymbol(parameters[0], CancellationToken.None);
            var symbol2       = semanticModel.GetDeclaredSymbol(parameters[1], CancellationToken.None);

            Assert.AreEqual(true, SymbolComparer.Equal((ISymbol)symbol1, (ISymbol)symbol1));
            Assert.AreEqual(false, SymbolComparer.Equal((ISymbol)symbol1, (ISymbol)symbol2));
            Assert.AreEqual(true, ParameterSymbolComparer.Equal(symbol1, symbol1));
            Assert.AreEqual(false, ParameterSymbolComparer.Equal(symbol1, symbol2));
            Assert.AreEqual(SymbolComparer.Default.GetHashCode(symbol1), ParameterSymbolComparer.Default.GetHashCode(symbol1));
            Assert.AreNotEqual(SymbolComparer.Default.GetHashCode(symbol1), ParameterSymbolComparer.Default.GetHashCode(symbol2));
        }