Re: Type convertsion from string



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







.



Relevant Pages

  • Re: Type convertsion from string
    ... Actually, to allow the widest set of usage and conversions, you should ... implementations are actually class properties. ... For the conversion, you might want to look at TypeConverter usage, ... public object Instance } ...
    (microsoft.public.dotnet.languages.csharp)
  • Proposal: String::Format::General
    ... It provides format string parsing and output assembly, you provide the code that implements the individual conversion characters. ... Format syntax is kind of a cross between sprintf and strftime, but how close it is to each of these depends on the semantics implemented by the user. ... Note that the following is pre-alpha documentation; the interface to the output conversion code has changed since yesterday, ... conversion character, and contain a number of optional fields which may ...
    (comp.lang.perl.modules)
  • Re: what if (f)printf returns EINTR ?
    ... vsnprintf - formatted output conversion ... int fprintf; ... write to the character string str. ...
    (comp.unix.programmer)
  • Re: Sets and portability (was) Re: Is ISO Pascal compatible with J&W (original) Pascal ?
    ... > So library code would be better if it could handle huge sets. ... widestring, in the future maybe also string). ... Note that ansi->wide conversion is codepage sensitive. ... compiler links in correct conversion code or table). ...
    (comp.lang.pascal.misc)
  • Re: Dynamic type conversion
    ... there is much more to dynamic conversion in the .Net ... Enumerations are considered a special form of a value type, ... This assumes the value is a string which can be converted to a Int32, byte, ... or enum that exists in the .Net Framework, or a custom type or enum ...
    (microsoft.public.dotnet.general)

Loading