Compare FxCop TypeNode to System.Type

183 Views Asked by At

I have an attribute named SearchableAttribute, which tags properties of classes. However, only properties of type string are allowed to be tagged with this [Searchable] attribute. In order to restict it, I am trying to write a CodeAnalysis rule which analyses the properties of my classes, checks for the existence of the [Searchable] attribute and creates a Problem if the property's type is not string.

This is what I have in my Rule class so far:

public override ProblemCollection Check(Member member) {
    PropertyNode property = member as PropertyNode;
    if (property == null) {
        return null;
    }

    if (property.Attributes.Any(a => a.Type.FullName.Equals((typeof(SearchableAttribute)).FullName)
            && !property.Type.FullName.Equals((typeof(string)).FullName)) {
        Resolution resolution = getResolution(property);
        Problem problem = new Problem(resolution);
        Problems.Add(problem);
    }

    return Problems;
}

While this actually does work, I can't believe ... no, I don't want to believe that I really have to compare the type's full names. I'm unable to figure out how to properly check for the existence of the SearchableAttribute and compare the property's type with string, however. Is there no clean and elegant solution?

1

There are 1 best solutions below

0
On

Microsoft.FxCop.Sdk.FrameworkTypes.String should do the trick.

FWIW, there is no real bridge between the framework types and their FxCop TypeNodes. All mappings need to be created based on name, which ideally includes the assembly fully qualified name. The FrameworkTypes properties basically give you shortcuts to accessing such mappings pre-created for you by the FxCop framework.