Question Defining a Generic Extension Method

njsokalski

Well-known member
Joined
Mar 16, 2011
Messages
102
Programming Experience
5-10
I am trying to write an Extension Method for IEnumerable(Of T). I will be using reflection on the Type T in the Extension Method to get a list of it's properties so that I can create an Anonymous Type that will include all those properties. I have written Extension Methods before, but never a Generic one. I know the declaration will be in a Module and have the Extension() Attribute, but I have having trouble figuring out how to make it Generic. Can somebody help me here? Thanks.
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,561
Location
Norway
Programming Experience
10+
Below you can see an example of a IEnumerable(Of T) extension signature. The generic method defines one or more generic parameters (type placeholders), in the example a T is defined. First parameter of extension is the type to extend, other parameters as needed.
Sub Something(Of T)(source As IEnumerable(Of T), parameter As Integer)

In this case an instance would call the extension like this:
instance.Something(3)

Extension Methods (Visual Basic)
Generic Procedures in Visual Basic
 
Top Bottom