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

0 Members and 1 Guest are viewing this topic.

gswang

  • Newt
  • Posts: 101
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: 829
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

  • SuperMod
  • Swamp Rat
  • Posts: 933
  • 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

Sometimes the question is more important than the answer.

gile

  • Water Moccasin
  • Posts: 2080
  • 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: 101
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

  • Water Moccasin
  • Posts: 2080
  • 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: 101
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

  • Bull Frog
  • Posts: 495
  • 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