I am looking for a solution to get all object creations inside a method. I came up with following solution to get a list of all creations:
var creations = MethodDeclaration.Body.DescendantNodes().OfType<ObjectCreationExpressionSyntax>();
But now I am looking for the identifier name(variable name). While debugging I figured out, that this variable has this information - but the identifier property is no accessible.
creations.First().Parent.Parent.Identifier.ValueText;
So i would be very pleased if someone has a solution for my problem.
EDIT
I am trying to analyze unittests. E.g.:
[TestMethod]
public void WarningOverReferencedTest()
{
var myVar = new MyObject();
var myVar1 = new MyObject1();
var myVar2 = new MyObject2();
var myVar3 = new MyObject();
var myVar4 = new MyObject3();
Assert.AreEqual(true, myVar.someProperty);
Assert.AreEqual(true, myVar3.someProperty2);
Assert.AreEqual(true, myVar1.someProperty);
Assert.AreEqual(true, myVar2.someProperty);
}
I want to count the references used in the Assert.AreEqual
checks. So the example from above should return 3, because myVar
and myVar3
are from the same type and myVar4
is not used in the Assert.AreEqual
check.