TypeDescriptor get Conveter attached to a property

  • Thread starter Thread starter Robert Ludig
  • Start date Start date
R

Robert Ludig

I have class that has a property wich has a custom typeconverter:

class A
{
private string _property;
[TypeConverter(typeof(MyTypeConverter))]
public string Property
{
get { return _property; }
}
}

How can I get an instance of this typeconverter? When I do

A a = new A();
TypeDescriptor.GetConverter(a);

I get an instance of the default Typeconverter attached to the class A
(as expected).
When I do

TypeDescriptor.GetConverter(TypeDescriptor.GetProperties(a)[0].GetValue(a));
I get an instance of the default Typeconverter of the class string (as
expected).

How would I retieve the instance of MyTypeConverter attached to
Property ?
 
Hi Robert,

PropertyDescriptor prop = TypeDescriptor.CreateProperty(typeof(A), "Property", typeof(string));
TypeConverter converterForProperty = prop.Converter;
 
The TypeConverter can be applied to a class (a type) in which caswe it will
be used when the type is converted, alternatively you can override the
TypeConverter associated with a particular type by declaring one on, say, a
property as you have done here.

In your example you would actually call GetConverter on the
PropertyDescriptor associated with your property such as

TypeConverter tc =
typeof(a).GetType().GetProperties("Property").GetConverter(); // not sure if
that's the exact syntax but you see the idea.


--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top