Search code examples
c#.netpdb-filesmono.cecilfody

Get SequencePoint for CustomAttribute in Fody/Mono.Cecil


I am writing a Fody Addin and I am able to inject my code and to provide error messages to the user. I am able to determine the sequence points of instructions, but I cannot find a way to find the sequence points of CustomAttributes.

I need to get this information to provide the debugger a hint where to find the location of an error, in case that an attribute has been applied wrongly.

So basically I have something like this:

[MyAttribute]
public void Test()
{

}

Now I want to get the SequencePoint of the MyAttribute attribute.

I can access the sequence point of instructions like this:

public static SequencePoint GetSP(MethodDefinition method)
{
    return method.Body.Instructions
        .Where(instruction => instruction.SequencePoint != null)
        .Select(instruction => instruction.SequencePoint)
        .FirstOrDefault();
}

That works fine for instructions but when I access an attribute I am not sure how to get the sequence point:

public static SequencePoint GetSP(MethodDefinition method)
{
    var attribute = method.CustomAttributes.First();
    // what to enter here to get SequencePoint of attribute?
}

Solution

  • This is not possible. Attributes don't have sequence points.

    I suggest you just use the first sequence point for the method instead.