TheSwamp

Code Red => .NET => Topic started by: BlackBox on January 31, 2016, 11:28:03 PM

Title: .NET Equivalent for LISP's vlax-method-applicable-p?
Post by: BlackBox 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
Title: Re: .NET Equivalent for LISP's vlax-method-applicable-p?
Post by: It's Alive! on February 01, 2016, 12:28:24 AM
Type.GetMembers ..?..
IsKindOf ..?.. if you know the base class where the method is applicable.
Title: Re: .NET Equivalent for LISP's vlax-method-applicable-p?
Post by: It's Alive! 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
Title: Re: .NET Equivalent for LISP's vlax-method-applicable-p?
Post by: kdub_nz on February 01, 2016, 05:12:14 AM

:)
Trademark :
Quote
[CommandMethod("DoIT")]
Title: Re: .NET Equivalent for LISP's vlax-method-applicable-p?
Post by: It's Alive! 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.
Title: Re: .NET Equivalent for LISP's vlax-method-applicable-p?
Post by: BlackBox 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