Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find generic invocations with a specific type argument in my source?

I have a generic method

public void Foo<T>(T arg) where T : ISomeInterface

This method is used quite a lot throughout the code, and I want to find where it is used with T being a specific type.

I can text-search for

"Foo<TheType>("

but most often the type argument has been omitted from the call (inferred). Is there any way I can find these method invocations in VS2010 or perhaps ReSharper?

like image 885
Anders Forsgren Avatar asked Jan 30 '12 08:01

Anders Forsgren


People also ask

How do I find the instance of a generic type?

The short answer is, that there is no way to find out the runtime type of generic type parameters in Java. I suggest reading the chapter about type erasure in the Java Tutorial for more details. A popular solution to this is to pass the Class of the type parameter into the constructor of the generic type, e.g.

What is generic type arguments?

The generic argument list is a comma-separated list of type arguments. A type argument is the name of an actual concrete type that replaces a corresponding type parameter in the generic parameter clause of a generic type. The result is a specialized version of that generic type.

What is a generic data type?

Definition: “A generic type is a generic class or interface that is parameterized over types.” Essentially, generic types allow you to write a general, generic class (or method) that works with different types, allowing for code re-use.


2 Answers

In the next version of ReSharper, this will be covered by "Search with Pattern" feature. http://youtrack.jetbrains.net/issue/RSRP-288080

like image 155
Evgeny Pasynkov Avatar answered Oct 17 '22 21:10

Evgeny Pasynkov


You could possibly get the compiler to help you find them using the old break-it-and-and-see-what-doesn't-compile approach: if the type T in question is your own code, try changing its definition so that it no longer implements ISomeInterface.

like image 41
jonsequitur Avatar answered Oct 17 '22 21:10

jonsequitur