对于属性setter PostSharp方面,调用泛型方法 [英] PostSharp aspect for property setters, calling generic method

查看:277
本文介绍了对于属性setter PostSharp方面,调用泛型方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有我们使用了一些MVC-like系统,其中的后裔每个属性是这样写的基本对象:

We have a base object we use for some MVC-like system, where each property in a descendant is written like this:

public String FirstName
{
    get { return GetProperty<String>("FirstName", ref _FirstName); }
    set { SetProperty<String>("FirstName", ref _FirstName, value); }
}

这既是调试和通知和验证目的而进行的。我们使用的getter来提醒我们的案件,其中已经明确地标记它是什么要读码(为了使基类能够调用它,只有当这些属性改变),并得到它错了,而我们使用的二传手对于属性更改通知,脏旗处理,验证等。

This is done both for debugging purposes and for notification and validation purposes. We use the getter to alert us of cases where code that has explicitly flagged what it is going to read (in order for the base class to be able to call it only when those properties change) and gets it wrong, and we use the setter for property change notifications, dirty-flag handling, validation, etc.

为了简单起见,我们假设这些方法的实现看起来是这样的:

For simplicity, let's assume the implementation of these methods looks like this:

protected T GetProperty<T>(String propertyName,
    ref T backingField)
{
    return backingField;
}

protected Boolean SetProperty<T>(String propertyName,
    ref T backingField,
    T newValue)
{
    backingField = newValue;
    return true;
}



有一个在这两个过程中的更多的代码,但是这个代码是不相关的我的问题,或者至少我希望如此。如果是,我将修改的问题。

There's more code in both of these of course, but this code is not relevant to my question, or at least I hope so. If it is, I'll modify the question.

不过,我想编写一个PostSharp方面,可自动实现了我的电话,自动性能,如这样的:

Anyway, I'd like to write a PostSharp aspect that automatically implements the calls for me, on automatic properties, like this:

public String FirstName { get; set; }



有没有人在那里,有一些想法,我怎么会去这样做?

Is there anyone out there that has some idea how I would go about doing this?

我已经OnMethodBoundaryAspect类我自己,而是调用通用实现与ref参数的艺术逃避我。

I have made OnMethodBoundaryAspect classes myself, but the art of calling the generic implementation with a ref parameter eludes me.

下面是这两个类,我想充实到的TestObject类自动调用的财产得到正确的方法和设置。

Here's the two classes, I'd like to augment the TestObject class to automatically call the correct method on property get and set.

public class BaseObject
{
    protected T GetProperty<T>(String propertyName,
        ref T backingField)
    {
        return backingField;
    }

    protected Boolean SetProperty<T>(String propertyName,
        ref T backingField,
        T newValue)
    {
        backingField = newValue;
    }
}

public class TestObject : BaseObject
{
    public String FirstName
    {
        get;
        set;
    }

    public String LastName
    {
        get;
        set;
    }
}





编辑:发布的 PostSharp论坛为好。

推荐答案

它不应该很简单。您覆盖OnEntry并设置基于你自己的代码的返回值。在最后你使用:

It should be very simple. You override the OnEntry and set the return value based on your own code. At the end you use:

eventArgs.ReturnValue = GetValue(x,y);  
eventArgs.FlowBehavior = FlowBehavior.Return;



这将有效地拦截原来get / set方法的调用。

which will effectively intercept the original Get/Set calls.

参阅这个博客这表明缓存方面使用相同的图案...

Refer to this blog which shows the cache aspect using the same pattern...

这篇关于对于属性setter PostSharp方面,调用泛型方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆