.NET 4.5 and caller information

Dec 12, 2012 at 8:51 PM

Hi.

Are there any plans to use CallerMemberNameAttribute in the NotifyPropertyChanged method of the view model base classes?

Coordinator
Dec 20, 2012 at 11:48 AM

I don't really want to use that, as it would be putting something quite 4.5 specific in there. 

 

Also I do like the ability to notify different properties, and maybe even more than 1 at a time, which is why I never went for a DepedencyProperty type thing for Cinch. Read more here : http://awkwardcoder.blogspot.co.uk/2012/07/callermembername-not-that-great-for.html

Dec 20, 2012 at 2:31 PM

I agree with your regards to .NET 4.5, however it is possible to use Caller Info without moving to .NET 4.5. Just create a file with the following content in your .NET 4 project (only tested in WPF):

----------------------------------------------------------------------------------------

using System;

using System.Runtime;

 

namespace System.Runtime.CompilerServices

{

    /// <summary>

    /// Allows you to obtain the method or property name of the caller to the method.

    /// </summary>

    [AttributeUsage(AttributeTargets.Parameter, Inherited = false)]

    [__DynamicallyInvokable]

    public sealed class CallerMemberNameAttribute : Attribute

    {

        [__DynamicallyInvokable]

        [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]

        public CallerMemberNameAttribute()

        {

        }

    }

 

    /// <summary>

    /// Allows you to obtain the full path of the source file that contains the caller. This is the

    /// file path at the time of compile.

    /// </summary>

    [AttributeUsage(AttributeTargets.Parameter, Inherited = false)]

    [__DynamicallyInvokable]

    public sealed class CallerFilePathAttribute : Attribute

    {

        [__DynamicallyInvokable]

        [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]

        public CallerFilePathAttribute()

        {

        }

    }

 

    /// <summary>

    /// Allows you to obtain the line number in the source file at which the method is called.

    /// </summary>

    [AttributeUsage(AttributeTargets.Parameter, Inherited = false)]

    [__DynamicallyInvokable]

    public sealed class CallerLineNumberAttribute : Attribute

    {

        [__DynamicallyInvokable]

        [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]

        public CallerLineNumberAttribute()

        {

        }

    }

}

 

internal sealed class __DynamicallyInvokableAttribute : Attribute

{

    [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]

    public __DynamicallyInvokableAttribute()

    {

    }

}

----------------------------------------------------------------------------------------

As regards to your second argument:, I also like use a syntax where multiple properties are stated, but that doesn't take anything away from the Caller Info version of sending property changed, it only extends the toolkit a developer can use when sending those events.

using System;
using System.Runtime;
namespace System.Runtime.CompilerServices
{
    /// <summary>
    /// Allows you to obtain the method or property name of the caller to the method.
    /// </summary>
    [AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
    [__DynamicallyInvokable]
    public sealed class CallerMemberNameAttribute : Attribute
    {
        [__DynamicallyInvokable]
        [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
        public CallerMemberNameAttribute()
        {
        }
    }
    /// <summary>
    /// Allows you to obtain the full path of the source file that contains the caller. This is the
    /// file path at the time of compile.
    /// </summary>
    [AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
    [__DynamicallyInvokable]
    public sealed class CallerFilePathAttribute : Attribute
    {
        [__DynamicallyInvokable]
        [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
        public CallerFilePathAttribute()
        {
        }
    }
    /// <summary>
    /// Allows you to obtain the line number in the source file at which the method is called.
    /// </summary>
    [AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
    [__DynamicallyInvokable]
    public sealed class CallerLineNumberAttribute : Attribute
    {
        [__DynamicallyInvokable]
        [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
        public CallerLineNumberAttribute()
        {
        }
    }
}
internal sealed class __DynamicallyInvokableAttribute : Attribute
{
    [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
    public __DynamicallyInvokableAttribute()
    {
    }
}
Coordinator
Dec 30, 2012 at 11:01 AM

Sorry about taking so long to get back to you on this, I tried using my IPhone but it crapped out on me.

I totally agree you could do this, and there is nothing stopping anyone doing this, and since the Cinch VM base classes are partial, you could indeed create a new method called "NotifyUsingCaller" or something. Nothing stopping you doing just that.

For me I just want to keep Cinch as clean as possible.