Author Topic: cool extension methods  (Read 6133 times)

0 Members and 1 Guest are viewing this topic.

It's Alive!

  • BricsCAD
  • Needs a day job
  • Posts: 6929
  • AKA Daniel
Re: cool extension methods
« Reply #15 on: February 12, 2009, 01:37:30 PM »
Nice work Gile !  8-)

He are mine for the week  :lol:



Code: [Select]
namespace ExecMethod
{

  public static class ExecMethods
  {
    [DebuggerHidden]
    public static void ThrowIfNull(this Object src)
    {
      if (src == null)
        throw new System.ArgumentNullException();
    }

    [DebuggerHidden]
    public static void ThrowIfNull(this Object src, string str)
    {
      if (src == null)
        throw new System.ArgumentNullException(str);
    }

   
    public static void AddRange<T>(this System.Collections.IList m_this, IEnumerable<T> items)
    {
      if (items != null)
      {
        foreach (T e in items)
          m_this.Add(e);
      }
    }

    public static void AddRange<T>(this System.Collections.IList m_this, System.Collections.IEnumerable items)
    {
      if (items != null)
      {
        foreach (T e in items)
          m_this.Add(e);
      }
    }

    public static void ForEach<T>(this System.Collections.IList m_this, Action<T> action)
    {
      if (action != null)
      {
        foreach (T e in m_this)
          action(e);
      }
    }
  }


  public static class Commands
  {


   
...
        ObjectIdCollection id = new ObjectIdCollection();
        ObjectIdCollection id2 = new ObjectIdCollection();

        id.ThrowIfNull();
        id2.ThrowIfNull();

        id.AddRange <ObjectId>(id2);
        id.ForEach<ObjectId>(p => ed.WriteMessage("\n{0}", p));
...
         
  }
}

gile

  • Water Moccasin
  • Posts: 2222
  • Marseille, France
Re: cool extension methods
« Reply #16 on: February 13, 2009, 02:54:57 PM »
Thanks Daniel,

The GeomExt and Triangle2d have grown a little and a new Triangle3d class is born.

Let me know if there's something wrong anf d if you find this usefull.


EDIT: I saw a little mistake in Triangle2d.GetAngleAt() have to replace (ang > pi * 2) with (ang > pi)
« Last Edit: February 13, 2009, 03:38:22 PM by gile »
Speaking English as a French Frog

TonyT

  • Guest
Re: cool extension methods
« Reply #17 on: February 13, 2009, 09:06:59 PM »

He are mine for the week  :lol:

Code: [Select]
   
   public static void ForEach<T>(this System.Collections.IList m_this, Action<T> action)
   {
      if (action != null)
      {
         foreach (T e in m_this)
           action(e);
      }
   }
 


A tip:

     public static void ForEach<T>( this IEnumerable target, Action<T> action )
     {
          // use with any IList, ICollection, or IEnumerable
     }

« Last Edit: February 13, 2009, 09:56:17 PM by TonyT »

It's Alive!

  • BricsCAD
  • Needs a day job
  • Posts: 6929
  • AKA Daniel
Re: cool extension methods
« Reply #18 on: February 13, 2009, 09:42:02 PM »
You are right!
Thanks Tony  8-)

It's Alive!

  • BricsCAD
  • Needs a day job
  • Posts: 6929
  • AKA Daniel
Re: cool extension methods
« Reply #19 on: February 13, 2009, 09:44:18 PM »
Another one  :laugh:

Code: [Select]
   public static IEnumerable<T> Where<T>(this System.Collections.IEnumerable m_this, Func<T, bool> predicate)
    {
      foreach (T element in m_this)
      {
        if (predicate(element) == true)
          yield return element;
      }
    }

Code: [Select]
public static class Commands
  {
    [LispFunction("doit")]
    public static object doit(ResultBuffer args)
    {
      IEnumerable<TypedValue> strings =
        args.Where<TypedValue>(X => X.TypeCode == (short)LispDataType.Text);

      return strings.Count();
    }
  }


TonyT

  • Guest
Re: cool extension methods
« Reply #20 on: February 14, 2009, 02:47:49 PM »
Or, you can just use Cast<T>():

Code: [Select]

void CastExample()
{
   TypedValue[] values = new TypedValue[] {
      new TypedValue(0, "FIRST"),
      new TypedValue(1, "SECOND"),
      new TypedValue(1, "THIRD")};

   ResultBuffer buffer = new ResultBuffer( values );

   foreach( TypedValue val in from item in buffer.Cast<TypedValue>()
                              where item.TypeCode == 1
                              select item )
   {
      Console.WriteLine( val.ToString() );
   }

   // or this way:

   foreach( TypedValue val in buffer.Cast<TypedValue>().Where( v => v.TypeCode == 1 ) )
      Console.WriteLine( val.ToString() );

}


Another one  :laugh:

Code: [Select]
   public static IEnumerable<T> Where<T>(this System.Collections.IEnumerable m_this, Func<T, bool> predicate)
    {
      foreach (T element in m_this)
      {
        if (predicate(element) == true)
          yield return element;
      }
    }

Code: [Select]
public static class Commands
  {
    [LispFunction("doit")]
    public static object doit(ResultBuffer args)
    {
      IEnumerable<TypedValue> strings =
        args.Where<TypedValue>(X => X.TypeCode == (short)LispDataType.Text);

      return strings.Count();
    }
  }