属性设置器的 PostSharp 方面,调用通用方法
我们有一个用于某些类似 MVC 的系统的基础对象,其中后代中的每个属性都按如下方式编写:
public String FirstName
{
get { return GetProperty<String>("FirstName", ref _FirstName); }
set { SetProperty<String>("FirstName", ref _FirstName, value); }
}
这样做既是为了调试目的,也是为了通知和验证目的。 我们使用 getter 来提醒我们,当代码明确标记要读取的内容(以便基类仅在这些属性更改时才能调用它)并出错时,我们使用 setter用于属性更改通知、脏标志处理、验证等。
为了简单起见,我们假设这些方法的实现如下所示:
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;
}
当然,这两个方法中都有更多代码,但此代码与我的问题无关,或者在至少我希望如此。 如果是的话我会修改问题。
无论如何,我想编写一个 PostSharp 方面,它可以在自动属性上自动为我实现调用,如下所示:
public String FirstName { get; set; }
是否有人知道我将如何执行此操作?
我自己创建了 OnMethodBoundaryAspect 类,但是使用 ref 参数调用通用实现的艺术却让我困惑。
这是两个类,我想扩展 TestObject 类以自动调用属性获取和设置的正确方法。
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 论坛 也是如此。
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); }
}
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.
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?
I have made OnMethodBoundaryAspect classes myself, but the art of calling the generic implementation with a ref parameter eludes me.
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;
}
}
Edit: Posted on PostSharp forum as well.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
应该很简单。 您可以重写 OnEntry 并根据您自己的代码设置返回值。 最后使用:
它将有效拦截原始的 Get/Set 调用。
请参阅此博客,其中使用相同的模式显示了缓存方面...
It should be very simple. You override the OnEntry and set the return value based on your own code. At the end you use:
which will effectively intercept the original Get/Set calls.
Refer to this blog which shows the cache aspect using the same pattern...