Author Topic: .NET Equivalent for LISP's vlax-method-applicable-p?  (Read 1006 times)

0 Members and 1 Guest are viewing this topic.

BlackBox

  • King Gator
  • Posts: 3745
.NET Equivalent for LISP's vlax-method-applicable-p?
« on: January 31, 2016, 11:28:03 PM »
Silly question after way too long away from .NET API, and admittedly my second (okay third) triple of Bombay Saphire + tonic, with Marzzetta olives marinated in vermouth... Mmmm....


What is / Is there - an .NET equivalent to LISP's vlax-method-applicable-p?

Basically, I want to iterate a selection set, and only for objects where the desired method is applicable, want to invoke said Method.

Cheers
« Last Edit: January 31, 2016, 11:34:56 PM by BlackBox »
"How we think determines what we do, and what we do determines what we get."

It's Alive!

  • BricsCAD
  • Needs a day job
  • Posts: 6963
  • AKA Daniel
Re: .NET Equivalent for LISP's vlax-method-applicable-p?
« Reply #1 on: February 01, 2016, 12:28:24 AM »
Type.GetMembers ..?..
IsKindOf ..?.. if you know the base class where the method is applicable.

It's Alive!

  • BricsCAD
  • Needs a day job
  • Posts: 6963
  • AKA Daniel
Re: .NET Equivalent for LISP's vlax-method-applicable-p?
« Reply #2 on: February 01, 2016, 04:27:04 AM »
maybe something like

Code - C#: [Select]
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using Autodesk.AutoCAD.DatabaseServices;
  5. using Autodesk.AutoCAD.Runtime;
  6.  
  7.  
  8. namespace VlaxMethodApplicableP
  9. {
  10.     public class Commands
  11.     {
  12.         static HashSet<Type> Tset = new HashSet<Type>();
  13.  
  14.         static bool HasMethod(DBObject type, string name)
  15.         {
  16.             Type t = type.GetType();
  17.             if (Tset.Contains(t))
  18.                 return true;
  19.             foreach (MethodInfo info in t.GetMethods())
  20.             {
  21.                 if (info.Name == name)
  22.                 {
  23.                     Tset.Add(t);
  24.                     return true;
  25.                 }
  26.             }
  27.             return false;
  28.         }
  29.    
  30.         [CommandMethod("DoIT")]
  31.         public static void AndDoIt()
  32.         {
  33.             ObjectIdCollection ids = new ObjectIdCollection();
  34.             Database db = HostApplicationServices.WorkingDatabase;
  35.             using (DBObject sp = db.CurrentSpaceId.Open(OpenMode.ForRead))
  36.             {
  37.                 foreach (ObjectId id in sp as BlockTableRecord)
  38.                 {
  39.                     using (DBObject obj = id.Open(OpenMode.ForRead))
  40.                     {
  41.                         if (HasMethod(obj, "get_StartPoint"))
  42.                             ids.Add(id);
  43.                     }
  44.                 }
  45.             }
  46.         }
  47.     }
  48. }
  49.  

added type cache
« Last Edit: February 01, 2016, 05:19:17 AM by nullptr »

kdub

  • Mesozoic relic
  • SuperMod
  • Swamp Rat
  • Posts: 1365
  • class keyThumper<T>:ILazy<T>
Re: .NET Equivalent for LISP's vlax-method-applicable-p?
« Reply #3 on: February 01, 2016, 05:12:14 AM »

:)
Trademark :
Quote
[CommandMethod("DoIT")]
called Kerry in my other life

Sometimes the question is more important than the answer.
#ridesober

It's Alive!

  • BricsCAD
  • Needs a day job
  • Posts: 6963
  • AKA Daniel
Re: .NET Equivalent for LISP's vlax-method-applicable-p?
« Reply #4 on: February 01, 2016, 05:22:04 AM »

:)
Trademark :
Quote
[CommandMethod("DoIT")]

I made it to DoIt45 once, but then I totally forgot what DoIt27 did.

BlackBox

  • King Gator
  • Posts: 3745
Re: .NET Equivalent for LISP's vlax-method-applicable-p?
« Reply #5 on: February 01, 2016, 11:27:54 AM »
maybe something like

Thanks, nullptr - this is very helpful.

Cheers




:)
Trademark :
Quote
[CommandMethod("DoIT")]

I made it to DoIt45 once, but then I totally forgot what DoIt27 did.

... 'It', obviously. :-D
"How we think determines what we do, and what we do determines what we get."