Re: Passing property Accesor as Function`

Ryan Heath <[email protected]>
Newsgroups gmane.comp.windows.devel.dotnet.clr
Message-ID <[email protected]>
Without reflection you can get as close as this:

delegate void SetHandler<T>(T value);
public void SetProperty<T>(object o, string property, T value)
{
  SetHandler<T> sh =
(SetHandler<T>)Delegate.CreateDelegate(typeof(SetHandler<T>), o,
"set_" + property);
  sh( value);
}

SetProperty<double>( person, "HourlyRate", 42);

But you would still not have compile time checks...

BTW
Is seams like you want databinding, no?

// Ryan

On Nov 19, 2007 7:21 AM, Brady Kelly <[email protected]> wrote:
> 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.