C # Extension methods (Extend method)
Add a method to the class without changing the original class.
1, the extension method must be written in a static class
2, the extension method must be a static method, although it is a static method, but this expansion method is extended for the object, can only be called by the object.
public static class name
{
public static return value method name (this is the type object name to extend [, parameter list])
{
}
}
Static Class Extend
{
public static TSource test123<tsource> (this ienumerable<tsource> source, Func<tsource, bool> predicate)
{
foreach (TSource item in Source)
{
if (predicate (item))
{
return (item);
}
}
throw new Exception ("not Found!");
}
}
List<int> ints =new list<int> {1,2,3,4,5,6};
int nret = INTs. Test123 (o = > 5);//nret=6
Attached: extension classes for NHibernate
Using NHibernate;
Using Nhibernate.type;
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Linq.Expressions;
Using System.Runtime.CompilerServices;
Namespace Nhibernate.linq
{
public static Class Linqextensionmethods
{
public static iqueryable<t> cacheable<t> (this iqueryable<t> query);
public static iqueryable<t> cachemode<t> (this iqueryable<t> query, CacheMode CacheMode);
public static iqueryable<t> cacheregion<t> (this iqueryable<t> query, string region);
public static T-mappedas<t> (This t parameter, IType type);
public static iqueryable<t> Query<t> (this isession session);
public static iqueryable<t> Query<t> (this istatelesssession session);
public static iqueryable<t> Query<t> (this isession session, String entityname);
public static iqueryable<t> Query<t> (this istatelesssession session, String entityname);
public static iqueryable<t> timeout<t> (this iqueryable<t> query, int Timeout);
public static ienumerable<t> tofuture<t> (this iqueryable<t> query);
public static ifuturevalue<t> tofuturevalue<t> (this iqueryable<t> query);
public static ifuturevalue<tresult> Tofuturevalue<t, tresult> (this iqueryable<t> query, Expression <func<iqueryable<t>, tresult>> selector);
}
}
C # Extension methods (Extend method)