English 中文(简体)
为什么如此缓慢? 指定财产 改用StackTrace
原标题:Just how slow is this? INotifyPropertyChanged using the StackTrace

今天,我采用了一种令人感兴趣的方法,来实施“创新”接口。 我们不是通过变换的地物名称,就是说呼声,而是可以简单地叫RaisePropertyChanged();从地块上说,这一呼吁是毫无意义的。 这是RaisePropertyChanged()方法中的法典:

public virtual void RaisePropertyChanged()
{
    var frames = new System.Diagnostics.StackTrace();
    for (var i = 0; i < frames.FrameCount; i++)
    {
        var frame = frames.GetFrame(i).GetMethod() as MethodInfo;
        if (frame != null)
            if (frame.IsSpecialName && frame.Name.StartsWith("set_"))
            {
                RaisePropertyChanged(frame.Name.Substring(4));
                return;
            }
    }
    throw new InvalidOperationException("NotifyPropertyChanged() can only by invoked within a property setter.");
}

这是一项将通知其受抚养人其变更的财产:

public string MyProperty
{
    get { return _myField; }
    set
    {
        _myField= value;
        RaisePropertyChanged();
    }
}

虽然我觉得这种做法令人感兴趣,但我认为,如果财产经常发生变化,或者如果我们的申请中的每一财产都采用这种做法来通知其变化,那么绩效惩罚可能很严重。

I d like to hear your opinions. (there is no longer the community-wiki checkbox?) Would this approach be very inefficient?

资料来源:

最佳回答

我刚刚用 Wim Coen swer在使用;MethodImpl属性。 我对这是否是一项可靠的停火工作感到怀疑。

成果:

Raised event using reflection 1000 times in 25.5334 ms.
Raised event WITHOUT reflection 1000 times in 0.01 ms.

因此,你可以看到,“热”解决办法的成本<>约为2 500倍。

That s the proportional answer, anyway. I personally dislike this idea (clever though it may be) for reasons quite beyond performance issues alone; but, obviously, it s your call.


rel=“nofollow noretinger”> 我感到不得不就这一写一个博客员额,特别是,尽管业绩受到打击,但一些开发商仍会试图采用类似的做法。

不管你是否同意我对这个问题的看法(我认识到,在绝对<>><>>条款中,所打的履约情况很小),我觉得,real<> /em>,对这一想法的杀戮是,为了使它更具有远距离的活力,就必须分解你打算称之为<代码>的所有财产。 RaisePropertyChanged with the MethodImpl Depende, through MethodImplOptions. NoInlining ..., which, right where, negates whatever打上了你本来获得的节余。

因此,你在发展时间留下了净损失(然而,在很多情况下,你将整条字打上<条码>。 如果你问我,走这条路就没有什么理由。

问题回答

叶,这似乎像许多工作一样,其代价是缓慢的,而且你有可能把这种方法放在一边。

如果你想这样做,我就建议采用“<条码>[MethodImplAttribute]

我的建议是采用动态程序,因为其代价比这种办法要容易得多,而且要快得多。 唯一的下端是,你必须指明这一方法是虚拟的。 例如,在我的工作进展中,我具体说明了一种美术定义,并对我的财产加以约束,这里是我的公证员。

    private static void SetterInterceptor<T, TProperty>(ProxiedProperty<T, TProperty> property, T target, TProperty value) where T:class,IAutoNotifyPropertyChanged
    {
        TProperty oldValue;
        if (!EqualityComparer<TProperty>.Default.Equals(oldValue = property.GetMethod.CallBaseDelegate(target), value))
        {
            property.SetMethod.CallBaseDelegate(target, value);
            target.OnPropertyChanged(new PropertyChangedEventArgs(property.Property.Name));
        }
    }

然后,它只是一个在Im有兴趣代表的土地上的 for。 创造力以履行约束性义务。

我没有明确的证明,但我认为是,这样做非常昂贵,特别是如果每个财产这样做的话。

业绩的确很重要,因为相关条款规定,它确实没有工作:

Apparently this is a method that has been tried in the past. As Sergey Barskiy pointed out, the JITter will likely inline the method and break the stack frame.

我喜欢的另一种做法是:

You can get the property name using reflection on a lambda function that calls the property getter.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...