How to determine the control has the specific properties or events or not?

  • Thread starter Thread starter ABC
  • Start date Start date
A

ABC

I want to check the form's controls have or not the specific properties or
events. How to determine or gather the properties list under the run-time
environment?
 
ABC,

You want to use Reflection for this. First, get the type of the object
through a call to GetType. Once you do this, you will want to call
GetProperties or GetEvents to get the properties or events.

Hope this helps.
 
I am a newie about Reflection. I have not any ideas about Reflection
concept. May I have some tutorials or artists from internet? Any
recommandation?


Nicholas Paldino said:
ABC,

You want to use Reflection for this. First, get the type of the object
through a call to GetType. Once you do this, you will want to call
GetProperties or GetEvents to get the properties or events.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

ABC said:
I want to check the form's controls have or not the specific properties or
events. How to determine or gather the properties list under the run-time
environment?
 
What exactly are you trying to do? Simply knowing if something has a
particular property does not help if you do not know the behavior, and
reflection will not identify the behavior. If you provide more information,
than a simpler solution might work.

ABC said:
I am a newie about Reflection. I have not any ideas about Reflection
concept. May I have some tutorials or artists from internet? Any
recommandation?


Nicholas Paldino said:
ABC,

You want to use Reflection for this. First, get the type of the
object through a call to GetType. Once you do this, you will want to
call GetProperties or GetEvents to get the properties or events.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

ABC said:
I want to check the form's controls have or not the specific properties
or events. How to determine or gather the properties list under the
run-time environment?
 
ABC -

Reflection allows you to programatically examine and work with objects
at runtime. This is a very powerful feature of .NET which allows you
to interact with objects which you may not know how to work with at
design time. With reflection, you can obtain information about
methods, constructors, fields, events, anything that is contained
within an object. Also, the objects which you wish to explore and work
with don't necessarily need to be public, so you can, for example, set
the value of a private field of an object even if it's marked as
private!

For your specific question, the code is relatively simple. For
example:

public bool HasProperty(object control, string propertyName)
{
return control.GetType().GetProperty(propertyName) != null;
}

public bool HasEvent(object control, string eventName)
{
return control.GetType().GetEvent(eventName) != null;
}

So, for example, if you want to know if a control, has a property named
"Text", you'd simply do:

bool hasText = HasProperty(myControl, "Text");

If you want to know if it has an event called "Clicked":

bool hasClicked = HasProperty(myControl, "Clicked");


Hope this helps!

Brant Estes
Senior Consultant
Magenic Technologies





I am a newie about Reflection. I have not any ideas about Reflection
concept. May I have some tutorials or artists from internet? Any
recommandation?


Nicholas Paldino said:
ABC,

You want to use Reflection for this. First, get the type of the object
through a call to GetType. Once you do this, you will want to call
GetProperties or GetEvents to get the properties or events.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

ABC said:
I want to check the form's controls have or not the specific properties or
events. How to determine or gather the properties list under the run-time
environment?
 
Back
Top