Here's an example:

    class Property
    {
        private readonly PropertyInfo _propertyInfo;

        public Property(LambdaExpression property)
        {
            Requires.NotNull(property, "property");

            var body = property.Body as MemberExpression;
            if (body == null)
                throw new ArgumentException("'property' should be a 
MemberException");

            _propertyInfo = (PropertyInfo)body.Member;
        }

        public string Name
        {
            get { return _propertyInfo.Name; }
        }

        public PropertyInfo PropertyInfo
        {
            get { return _propertyInfo; }
        }

        public static implicit operator string(Property property)
        {
            return property.Name;
        }

        public static implicit operator PropertyInfo(Property property)
        {
            return property.PropertyInfo;
        }

        public static Property Of(Expression<Func<object>> property)
        {
            return new Property(property);
        }

        public static Property Of<T>(Expression<Func<T, object>> property)
        {
            return new Property(property);
        }
    }

You can then use this:

Foo foo = ...

DisplayMember = Property.Of(() => foo.Bar);


-----Original Message-----
From: [email protected] [mailto:[email protected]] On 
Behalf Of Arjang Assadi
Sent: Wednesday, May 26, 2010 1:10 PM
To: ozDotNet
Subject: Re: ASP.NET Control databinding, member properties, dynamically fetch 
property name?

Do you have some links or examples for us to look at? (For winforms too would 
be good) What are the goodness than one can benefit from. Are there any 
programming tools that are can be used with this?

Kind Regards

Arjang

On 27 May 2010 01:51, David Kean <[email protected]> wrote:
> I'm really interesting in hearing why people think this is a bad idea. 
> I've done this in a few code bases (not ASP.NET, but WinForms) and it 
> nothing but pure goodness from my perspective.
>
>
>
> From: [email protected] 
> [mailto:[email protected]]
> On Behalf Of Stephen Price
> Sent: Wednesday, May 26, 2010 3:10 AM
> To: ozDotNet
> Subject: Re: ASP.NET Control databinding, member properties, 
> dynamically fetch property name?
>
>
>
> So often those things are black and white. They can be done one way, 
> and the other way can be argued wrong. But then on the other hand 
> there are reasons why the other way can be right.
>
>
>
> When I come across people who believe in the inverse of myself it's 
> usually in the middle of something that needs to get done. Arguing the 
> point would lose time so I let it slide. Put it in the "do be 
> discussed later over a beer or food" where the full ramifications and 
> spiritual benefits of such code can be truly enjoyed. If all your 
> codebase was the way you'd write it then you might as well have 
> written it yourself. You can still be a craftsman and care about what 
> you do. :)
>
>
>
> On Wed, May 26, 2010 at 5:27 PM, Winston Pang <[email protected]> wrote:
>
> Sorry, I think I didn't make it clear, the person who asked me us to 
> do this was a Senior Developer on their team. They too could be 
> lurking these mailing list, which would be cool if they replied too! 
> :D
>
> So it's not so much about educating them... I'm sure they are "Senior"
> enough to know the ramifications, consdidering I did outline, it's 
> uncommon and also quite redundant. But anyways, there's no winner, 
> even if you outline all the cons to this, they are also a programmer 
> too, have you ever had disputes with other developers other doing one thing 
> over another way?
> Sometimes it gets resolved, sometimes people are just way too stubborn.
>
>
> On Wed, May 26, 2010 at 3:51 PM, Arjang Assadi 
> <[email protected]>
> wrote:
>
> Or
>
> Just an opportunity to do something different, that is what they want 
> that is what we will give them, or even better ask them why? where did 
> they get the idea to have it like that from and understand their real 
> concerns that has facilitated the odd requirements, maybe there is 
> method to their madness or maybe they have misunderstood something and 
> need to be helped out of it.
>
> No client knows really what they want, only what they think that they 
> want, we (the programmers) are their guides and confidants, we (as
> programmers) have to hold their hands and help them out, after all if 
> we don't then who will?
>
> Please ask and find out their real problem and what will satisfaction 
> of this requirement give them, and report back here! :)
>
> Let's find out the real problem.
>
> Kind Regards
>
> Arjang
>
> On 26 May 2010 14:12, Winston Pang <[email protected]> wrote:
>> Well, firstly. The client is govt, and we're really dealing with 
>> their internal IT team, who would eventually do supporting for the system.
>>
>> I have no say in whether we do it or not, I've told them, it's 
>> redundant, and uncommon, and a bit too much. I've done all I can, 
>> can't fight anymore about it.
>>
>> I just wanted to see what people though of it, I quite frankly think 
>> it's stupid and unnecessary.
>>
>>
>>
>> On Wed, May 26, 2010 at 2:05 PM, mike smith <[email protected]> wrote:
>>>
>>> On 26 May 2010 08:35, Winston Pang <[email protected]> wrote:
>>> > Hi guys,
>>> >
>>> >
>>> > This is more of a question of whether or not this sounds feasible 
>>> > and has anyone seen anyone do this:
>>> >
>>> >
>>> > Typically with most ASP.NET controls, lets take the ComboBox for 
>>> > an example, it'll be like
>>> >
>>> > this.comboBox.DataSource = someList; this.comboBox.DisplayMember = 
>>> > "Property1"; this.comboBox.ValueMember = "Property2";
>>> >
>>> > The client we're dealing with, has specifically told us to not 
>>> > "hard-code"
>>> > these property names, and to use reflection, through lambda 
>>> > expressions to derive the property name.
>>>
>>> I find a lot of use can be gained in such cases in asking the client 
>>> why they want to do things a certain way at the outset, rather than 
>>> blindly doing what they ask.  Sounds a lot like COM late binding 
>>> (aka everything old is new again)
>>>
>>> >
>>> > Firstly, do you think this will add a lot of overhead? I'm 
>>> > guessing it'll be ok-ish, considering ASP.NET MVC uses it a lot.
>>> >
>>> > Also, has anyone seen people do it this way?
>>> >
>>> > It this a stupid thing to do? I think it has it's merits to an 
>>> > extent, but it sure doesn't seem common to me.
>>> >
>>> >
>>> > Cheers,
>>> >
>>> >
>>> > Winston
>>> >
>>>
>>>
>>>
>>> --
>>> Meski
>>>
>>> "Going to Starbucks for coffee is like going to prison for sex. 
>>> Sure, you'll get it, but it's going to be rough" - Adam Hills
>>
>>
>
>
>
>

Reply via email to