Some of my commonly-used extension methods:
// Should be self-explainatory:
public static class DoubleExtender
{
public static string ToDistance( this double value )
{
return Converter.DistanceToString( value, DistanceUnitFormat.Current, -1 );
}
public static string ToDistance( this double value, DistanceUnitFormat format, int precision )
{
return Converter.DistanceToString( value, format, precision );
}
// TODO: ToAngle()
}
public static class PolylineHelper
{
public static Curve GetCurveAt( this Polyline pline, int index )
{
Entity ent = pline.GetSubentity(
new FullSubentityPath(
new ObjectId[] { pline.ObjectId },
new SubentityId( SubentityType.Edge, index + 1 ) ) );
if( ent != null )
{
Curve curve = ent as Curve;
if( curve != null )
return curve;
ent.Dispose();
}
return null;
}
public static Curve GetCurveAt( this Polyline pline, Point3d pointOnSegment )
{
return GetCurveAt( pline, (int) Math.Truncate( pline.GetParameterAtPoint( pointOnSegment ) ) );
}
public static Curve GetCurveNearestTo( this Polyline pline, Point3d point )
{
return GetCurveAt( pline, pline.GetClosestPointTo( point, false ) );
}
public static Curve3d GetCurve3dAt( this Polyline pline, int index )
{
switch( pline.GetSegmentType( index ) )
{
case SegmentType.Arc:
return pline.GetArcSegmentAt( index );
case SegmentType.Line:
return pline.GetLineSegmentAt( index );
default:
throw new InvalidOperationException( "Unknown segment type" );
}
}
public static Curve2d GetCurve2dAt( this Polyline pline, int index )
{
switch( pline.GetSegmentType( index ) )
{
case SegmentType.Arc:
return pline.GetArcSegment2dAt( index );
case SegmentType.Line:
return pline.GetLineSegment2dAt( index );
default:
throw new InvalidOperationException( "Unknown segment type" );
}
}
}
public static class BlockReferenceExtensionMethods
{
public static string DefinitionName( this BlockReference target )
{
if( target.IsDynamicBlock )
{
using( BlockTableRecord obj = target.DynamicBlockTableRecord.Open( OpenMode.ForRead, true, true ) as BlockTableRecord )
return obj.Name;
}
return target.Name;
}
}
/// This class allows you to tell if a List<T>
/// was modified between two points in time:
///
/// Example:
///
/// List<int> myList = new List<int>();
/// myList.Add( 1 );
/// myList.Add( 2 );
/// myList.Add( 3 );
///
/// int version = myList.GetVersion();
///
/// SomeVirtualMethod( myList ); // called method may modify the list
///
/// if( myList.IsModified( version ) )
/// Console.WriteLine("SomeVirtualMethod() changed the list");
///
public static class ListExtender
{
public static int GetVersion<T>( this List<T> list )
{
FieldInfo fi = typeof( List<T> ).GetField( "_version", BindingFlags.Instance | BindingFlags.NonPublic );
if( fi != null )
{
return (int) fi.GetValue( list );
}
throw new InvalidOperationException();
}
public static bool IsModified<T>( this List<T> list, int oldVersion )
{
return list.GetVersion<T>() != oldVersion;
}
}
public static class QueueExtender
{
public static void Enqueue<T>( this Queue<T> queue, IEnumerable<T> items )
{
foreach( T item in items )
queue.Enqueue( item );
}
public static void Enqueue<T>( this Queue<T> queue, IEnumerable items )
{
foreach( object item in items )
queue.Enqueue( (T) item );
}
}
/// The most useful extension methods I know of, with the
/// caveat that they're probably unsuitable in performance-
/// critical scenarios.
///
/// Example:
///
/// BindingFlags flags = BindingFlags.Public | BindingFlags.InvokeMethod | BindingFlags.Static;
///
/// Instead of doing this:
///
/// bool isStatic = ( flags & BindingFlags.Static ) == BindingFlags.Static;
///
/// You can instead do this:
///
/// bool isStatic = flags.Includes( BindingFlags.Static );
///
/// Or:
///
/// bool isStatic = flags.And( BindingFlags.Static );
public static class EnumExtender
{
public static bool Includes( this Enum target, Enum flags )
{
if( target.GetType() != flags.GetType() )
throw new ArgumentException( "Enum type mismatch" );
long a = Convert.ToInt64( target );
long b = Convert.ToInt64( flags );
return (a & b) == b;
}
public static bool IncludesAny( this Enum target, Enum flags )
{
if( target.GetType() != flags.GetType() )
throw new ArgumentException( "Enum type mismatch" );
return ( Convert.ToInt64( target ) | Convert.ToInt64( flags ) ) != 0L;
}
public static bool And( this Enum target, Enum flags )
{
return Includes( target, flags );
}
public static bool Or( this Enum target, Enum flags )
{
return IncludesAny( target, flags );
}
}