Author Topic: How to determine whether an object has an property?  (Read 2601 times)

0 Members and 1 Guest are viewing this topic.

gswang

  • Newt
  • Posts: 117
How to determine whether an object has an property?
« on: June 01, 2017, 11:48:12 PM »
In .NET,How to determine whether an object has an property? like vlax-property-available-p in lisp.
(vlax-property-available-p obj 'Thickness)
Thank you very much!
« Last Edit: June 02, 2017, 12:45:07 AM by gswang »

Atook

  • Swamp Rat
  • Posts: 1027
  • AKA Tim
Re: How to determine whether an object has an attribute?
« Reply #1 on: June 02, 2017, 12:09:47 AM »
From Autodesk.

You can check the BlockTableRecord for .HasAttributes.

If so, you can access them via the blockreference.AttributeCollection.
« Last Edit: June 02, 2017, 02:13:58 AM by Atook »

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2125
  • class keyThumper<T>:ILazy<T>
Re: How to determine whether an object has an property?
« Reply #2 on: June 02, 2017, 12:37:19 AM »
Since attribute are attached to blocks ...

Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: How to determine whether an object has an property?
« Reply #3 on: June 02, 2017, 01:25:26 AM »
Hi,

You can use Reflection:

Code - C#: [Select]
  1. public bool IsAvailableProperty(object obj, string propertyName) =>
  2.     obj.GetType().GetProperties().Any(p => p.Name == propertyName);
« Last Edit: June 02, 2017, 01:42:56 AM by gile »
Speaking English as a French Frog

gswang

  • Newt
  • Posts: 117
Re: How to determine whether an object has an property?
« Reply #4 on: June 02, 2017, 08:13:05 AM »
Thank you, gile,
but i can't compile successfully, how to use reflection?

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: How to determine whether an object has an property?
« Reply #5 on: June 02, 2017, 08:32:42 AM »
You have to add a using System.Linq; directive and, if you're not using VS 2015 ou higher (C# 6), change the code as this:

Code - C#: [Select]
  1.         public bool IsAvailableProperty(object obj, string propertyName)
  2.         {
  3.             return obj.GetType().GetProperties().Any(p => p.Name == propertyName);
  4.         }
Speaking English as a French Frog

gswang

  • Newt
  • Posts: 117
Re: How to determine whether an object has an property?
« Reply #6 on: June 02, 2017, 09:02:01 AM »
Thank you very much, gile. I made it successfully! :2funny:

Bobby C. Jones

  • Swamp Rat
  • Posts: 516
  • Cry havoc and let loose the dogs of war.
Re: How to determine whether an object has an property?
« Reply #7 on: June 05, 2017, 04:08:11 PM »
In .NET,How to determine whether an object has an property? like vlax-property-available-p in lisp.
(vlax-property-available-p obj 'Thickness)
Thank you very much!

Nothing against reflection, it can be useful, but it seems in a type safe environment the better question asks if the object implements an interface that declares the desired property or method.
Bobby C. Jones