Define and call extension methods
Define a static class to include the extension method. This class must be visible to the client code.
Implement the extension method as a static method and make it have at least the same visibility as the inclusion class.
The first parameter of the method specifies the type of operation performed by the method. The parameter must start with the this modifier.
In the call code, add a using command to specify the namespace containing the extension method class.
Call the Extension Method in the same way as the instance method on the call type.
Note that the first parameter is not specified by the call code because it represents the type of the operator being applied and the compiler knows the object type. You only needNProvide real parameters for the two parameters.
Custom extension methods
Namespace lcctest
{
Public static class test
{
/// <Summary>
///
/// </Summary>
/// <Param name = "sd"> </param>
/// <Param name = "id"> </param>
/// <Param name = "value"> </param>
/// <Param name = "test"> </param>
/// <Returns> </returns>
Public static string TestHelper (this object sd, string id, string value, string test)
{
Return "successful ";
}
}
}
Call
Using lcctest; // reference the namespace
Namespace MvcApplication1.Controllers
{
Public class test
{
Public void test1 ()
{
String sd;
Sd. TestHelper ();
}
}
}