Re: Type convertsion from string
- From: "Andrus" <kobruleht2@xxxxxx>
- Date: Tue, 16 Oct 2007 14:05:50 +0300
Marc,
Thank you.
I created the following method but this causes error shown in comment.
How to fix ?
Is this best style to create this method for conversion user entered value ?
Andrus.
/// <summary>
/// Set value of property from string.
/// </summary>
/// <param name="obj">Object whose property should be set</param>
/// <param name="propertyName">Name of property to set</param>
/// <param name="propertyValue">User entered value</param>
public static void SetValue(object obj, string propertyName, string
propertyValue) {
//How to fix error in following line: 'System.Type' does not contain a
definition for 'GetPropertyDescriptor'
PropertyDescriptor p = obj.GetType().GetPropertyDescriptor();
ITypeDescriptorContext ctx = new SimpleContext(obj,p);
if (p.Converter.CanConvertFrom(ctx, typeof(string))) {
object val = p.Converter.ConvertFromString(ctx, propertyValue);
p.SetValue(obj, val);
}
else {
UIManager.OKGet( "Invalid entry");
}
}
[ImmutableObject(true)]
public class SimpleContext : ITypeDescriptorContext {
private readonly object instance;
private readonly PropertyDescriptor property;
public SimpleContext(object instance, PropertyDescriptor
property) {
this.instance = instance;
this.property = property;
}
public IContainer Container {get { return null; }}
public object Instance {get { return instance; }}
public void OnComponentChanged() {}
public bool OnComponentChanging() {return true;}
public PropertyDescriptor PropertyDescriptor { get { return
property; } }
public object GetService(Type serviceType) {return null;}
}}
"Marc Gravell" <marc.gravell@xxxxxxxxx> kirjutas sõnumis
news:1192508665.627282.289650@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Actually, to allow the widest set of usage and conversions, you should
be using PropertyDescriptor (not PropertyInfo); not all "property"
implementations are actually class properties. Think (non-typed)
DataRow, although many other examples (involving ICustomTypeDescriptor
or TypeDescriptionProvider) exist.
For the conversion, you might want to look at TypeConverter usage,
specifically (where p is a PropertyDescriptor):
ITypeDescriptorContext ctx = new SimpleContext(entityTmp,
p);
if (p.Converter.CanConvertFrom(ctx, typeof(string))) {
object val = p.Converter.ConvertFromString(ctx,
e.Value);
p.SetValue(entityTmp, val);
} else {
// something
}
where SimpleContext is something like:
[ImmutableObject(true)]
public class SimpleContext : ITypeDescriptorContext {
private readonly object instance;
private readonly PropertyDescriptor property;
public SimpleContext(object instance, PropertyDescriptor
property) {
this.instance = instance;
this.property = property;
}
public IContainer Container {get { return null; }}
public object Instance {get { return instance; }}
public void OnComponentChanged() {}
public bool OnComponentChanging() {return true;}
public PropertyDescriptor PropertyDescriptor { get { return
property; } }
public object GetService(Type serviceType) {return null;}
}
This does 3 things that Convert.ChangeType does not:
* respects a TypeConverter specified on the property itself (rather
than the type)
* passes context to the converter, which is sometimes required if the
converter uses property metadata
* (by doing the above) more closley represents how DataGridView (and
many other bindings) themselves work
There are corresponding .ConvertTo() methods for getting the string in
the first place, and an .IsValid() method for verifying values [but
this is really intended for known-list (StandardValues) lookups]. To
try the conversion I would simply catch the exception (if one). You'd
want to wrap the SetValue() too, since the property-setter could just
as legitimately throw an exception for any reason it likes.
Marc
.
- Follow-Ups:
- Re: Type convertsion from string
- From: Marc Gravell
- Re: Type convertsion from string
- References:
- Type convertsion from string
- From: Andrus
- Re: Type convertsion from string
- From: Jon Skeet [C# MVP]
- Re: Type convertsion from string
- From: Andrus
- Re: Type convertsion from string
- From: Marc Gravell
- Type convertsion from string
- Prev by Date: CreateInstance without ReflectionPermission
- Next by Date: Closing a TabPage, another approach, possible?
- Previous by thread: Re: Type convertsion from string
- Next by thread: Re: Type convertsion from string
- Index(es):
Relevant Pages
|
Loading