Identify IInvocationExpression method as particular library class method


I have daemon that provide some logic against the Moq framework.
How can I recognize that method in some IInvocationExpression corresponds exactly to method from library?
Currently I'm using some sort of approach:

var methodName = invocationExpression.InvocationExpressionReference.CurrentResolveResult.DeclaredElement.ToString()
if (methodName.StartsWith("Method:Moq.Mock.Of"))
     // do something

Is there more elegant (or correct) way?


Comment actions Permalink

You would do better to (safely) downcast the IDeclaredElement to IClrDeclaredElement and use IClrDeclaredElement.Module to get the assembly that the element is declared in. From this you can use IPsiModule.Name to get the name of the assembly, and compare with the assembly name. You can also try to downcast this to IAssemblyPsiModule, which would give you access to assembly information, such as location and assembly name, version, etc.

Comment actions Permalink

Thanks for reply!

Ok, using IClrDeclaredElement I can identify assembly (Moq). It saves me from some sort of mistakes.
I am still using strings for type name and method name. Is it the only way?

Comment actions Permalink

Yes, it's fine to use strings to compare assembly, type and method name, as long as you're using proper methods and properties, and not relying on ToString. After all, these names are just strings in the CLR...


Please sign in to leave a comment.