Click here to Skip to main content
15,888,454 members
Home / Discussions / C#
   

C#

 
GeneralRe: Subscribe to property changes using Reactive Extensions Pin
Kenneth Haugland29-Jan-16 4:47
mvaKenneth Haugland29-Jan-16 4:47 
GeneralRe: Subscribe to property changes using Reactive Extensions Pin
Richard Deeming29-Jan-16 5:03
mveRichard Deeming29-Jan-16 5:03 
GeneralRe: Subscribe to property changes using Reactive Extensions Pin
Kenneth Haugland29-Jan-16 5:22
mvaKenneth Haugland29-Jan-16 5:22 
AnswerRe: Subscribe to property changes using Reactive Extensions Pin
Richard Deeming29-Jan-16 3:31
mveRichard Deeming29-Jan-16 3:31 
GeneralRe: Subscribe to property changes using Reactive Extensions Pin
Kenneth Haugland29-Jan-16 4:56
mvaKenneth Haugland29-Jan-16 4:56 
AnswerRe: Subscribe to property changes using Reactive Extensions Pin
Sascha Lefèvre29-Jan-16 3:49
professionalSascha Lefèvre29-Jan-16 3:49 
GeneralRe: Subscribe to property changes using Reactive Extensions Pin
Kenneth Haugland29-Jan-16 4:59
mvaKenneth Haugland29-Jan-16 4:59 
GeneralRe: Subscribe to property changes using Reactive Extensions Pin
Sascha Lefèvre29-Jan-16 5:30
professionalSascha Lefèvre29-Jan-16 5:30 
Kenneth Haugland wrote:
But I cant get your last line of code to compile
Sorry, I renamed property to propertyExpr (because it's an Expression) without mentioning it.

Kenneth Haugland wrote:
And the result will only return the actual value thats changed, not the sender and event args.
That's the case for all the so far shown solutions in this thread. If you want to have the sender and event args you would have to change some stuff:
C#
public class PropertyChangedResult<TTarget, TProperty>
{
    public TTarget Target { get; private set; }
    public TProperty Value { get; private set; }
    public EventArgs EventArgs { get; private set; }

    public PropertyChangedResult(TTarget target, TProperty value, EventArgs e)
    {
        Target = target;
        Value = value;
        EventArgs = e;
    }
}

public static IObservable<PropertyChangedResult<TTarget, TProperty>> FromPropertyChanged<TTarget, TProperty>(TTarget target, Expression<Func<TTarget, TProperty>> propertyExpr)
{
    Contract.Requires(target != null);
    Contract.Requires(property != null);

    var body = propertyExpr.Body as MemberExpression;

    if (body == null)
        throw new ArgumentException("The specified expression does not reference a property.", "property");

    var propertyInfo = body.Member as PropertyInfo;

    if (propertyInfo == null)
        throw new ArgumentException("The specified expression does not reference a property.", "property");

    string propertyName = propertyInfo.Name;

    var propertyDescriptor = (from p in TypeDescriptor.GetProperties(target).Cast<PropertyDescriptor>()
                                where string.Equals(p.Name, propertyName, StringComparison.Ordinal)
                                select p)
            .Single();

    if (!propertyDescriptor.SupportsChangeEvents)
        throw new ArgumentException("The specified property does not support change events.", "property");

    return Observable.FromEvent<EventHandler, EventArgs>(
        propertyChangedHandler => (s, e) => propertyChangedHandler(e),
        h => propertyDescriptor.AddValueChanged(target, h),
        h => propertyDescriptor.RemoveValueChanged(target, h))
        .Select(e => new PropertyChangedResult<TTarget, TProperty>(target, propertyExpr.Compile()(target), e));
}
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

GeneralRe: Subscribe to property changes using Reactive Extensions Pin
Kenneth Haugland29-Jan-16 5:51
mvaKenneth Haugland29-Jan-16 5:51 
QuestionEnable / Disable DNS Client service (c#) Pin
Member 1041097228-Jan-16 22:21
Member 1041097228-Jan-16 22:21 
AnswerRe: Enable / Disable DNS Client service (c#) Pin
Nathan Minier29-Jan-16 2:05
professionalNathan Minier29-Jan-16 2:05 
GeneralRe: Enable / Disable DNS Client service (c#) Pin
Member 1041097229-Jan-16 5:21
Member 1041097229-Jan-16 5:21 
GeneralRe: Enable / Disable DNS Client service (c#) Pin
Nathan Minier29-Jan-16 5:28
professionalNathan Minier29-Jan-16 5:28 
AnswerRe: Enable / Disable DNS Client service (c#) Pin
Eddy Vluggen29-Jan-16 2:19
professionalEddy Vluggen29-Jan-16 2:19 
GeneralRe: Enable / Disable DNS Client service (c#) Pin
Member 1041097229-Jan-16 5:25
Member 1041097229-Jan-16 5:25 
QuestionHow to change utc Date in a normal Date Pin
Member 1191673528-Jan-16 2:20
Member 1191673528-Jan-16 2:20 
AnswerRe: How to change utc Date in a normal Date Pin
Sascha Lefèvre28-Jan-16 3:06
professionalSascha Lefèvre28-Jan-16 3:06 
GeneralRe: How to change utc Date in a normal Date Pin
jschell29-Jan-16 12:26
jschell29-Jan-16 12:26 
AnswerRe: How to change utc Date in a normal Date Pin
Eddy Vluggen28-Jan-16 3:19
professionalEddy Vluggen28-Jan-16 3:19 
AnswerRe: How to change utc Date in a normal Date Pin
Midi_Mick28-Jan-16 14:00
professionalMidi_Mick28-Jan-16 14:00 
AnswerRe: How to change utc Date in a normal Date Pin
John Torjo28-Jan-16 19:54
professionalJohn Torjo28-Jan-16 19:54 
AnswerRe: How to change utc Date in a normal Date Pin
Member 1191673528-Jan-16 20:41
Member 1191673528-Jan-16 20:41 
GeneralRe: How to change utc Date in a normal Date Pin
Richard MacCutchan28-Jan-16 22:38
mveRichard MacCutchan28-Jan-16 22:38 
AnswerRe: How to change utc Date in a normal Date Pin
jschell29-Jan-16 12:23
jschell29-Jan-16 12:23 
QuestionC# Application updated database. Pin
Ibrahim.elh28-Jan-16 0:07
Ibrahim.elh28-Jan-16 0:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.