Re: Passing property Accesor as Function`

Mark Brackett <[email protected]>
Newsgroups gmane.comp.windows.devel.dotnet.clr
Message-ID <[email protected]>
I think your real problem is making a generic handler is figuring out
the conversion functions. Once you have that, I don't see the difference
between what you have and:

pers.HourlyRate = PropertyParse<double?>(textRate.Text);

Besides maybe the additional type parameter.

My first stab is:

T PropertyParse<T>(string value) {
   return value.Length == 0 ? default(T) : (T) Convert.ChangeType(value,
typeof(T));
}

But ChangeType doesn't seem to work with nullables...Argh. So, how
about:

T PropertyParse<T>(string value) {
  Type realType = Nullable.GetUnderlyingType(typeof(T)) ?? typeof(T);
  return value.Length == 0 ? default(T) : (T) Convert.ChangeType(value,
realType);
}

Which passes the 2 minute SnippetCompiler test....Only downside is the
compiler isn't checking for a conversion operator because of ChangeType
- so you can get an InvalidCastException (of course, you already have to
deal with the FormatException with invalid input).

--MB

> -----Original Message-----
> From: Discussion of development on the .NET platform using any managed
> language [mailto:[email protected]] On Behalf Of Brady
> Kelly
> Sent: Monday, November 19, 2007 1:22 AM
> To: [email protected]
> Subject: [DOTNET-CLR] Passing property Accesor as Function`
> 
> Warning:  This is a purely academic and speculative post.
> 
> 
> 
> This has suddenly become a real 'would like to have' feature in the
> project
> I'm currently working on.  I know this isn't possible in standard C#,
> but
> I'm trying to find a hammer with the right weight and shape to beat
> this
> into some semblance of working.  What I would like to achieve is
> something
> like this, in the commented line:
> 
> 
> 
>     public class PersonForm
> 
>     {
> 
>         System.Windows.Forms.TextBox textRate = new TextBox();
> 
>         public void BuildPerson(Person person)
> 
>         {
> 
>             Person pers = new Person();
> 
>             pers.HourlyRate = textRate.Text.Length == 0 ? (double?)
> null :
> double.Parse(textRate.Text);
> 
>             // SetPropertyFromText(pers.HourlyRate, textRate.Text)
> 
>         }
> 
>     }
> 
> 
> 
> My SetPropertyFromText should take the pers.HourlyRate as a delegate
> parameter, not as a double?, determine the correct type conversion
> based on
> the type of the pers.HourlyRate, and set the property to the converted
> object value.
> 
> 
> 
> I can easily use a property name as my property parameter for
> SetPropertyFromText, but then I lose compile time checks.  Our
> generated
> DAL, nettiers, provides, for each entity, an Enum with all the column
> properties for each entity, and I think this is about as close as I've
> seen
> to what I want, but not quite there.
> 
> 
> 
> Any ideas on the matter?
> 
> 
> 
> 
> ===================================
> This list is hosted by DevelopMentor(r)  http://www.develop.com
> 
> View archives and manage your subscription(s) at
> http://discuss.develop.com

===================================
This list is hosted by DevelopMentor®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.