而不是这个..。
public string Text
{
get { return ViewState["Text"] as string; }
set { ViewState["Text"] = value; }
}我想要这个..
[ViewState]
public String Text { get; set; }这是可以做到的吗?
发布于 2009-11-04 22:27:38
如下所示:
public class BasePage: Page {
protected override Object SaveViewState() {
object baseState = base.SaveViewState();
IDictionary<string, object> pageState = new Dictionary<string, object>();
pageState.Add("base", baseState);
// Use reflection to iterate attributed properties, add
// each to pageState with the property name as the key
return pageState;
}
protected override void LoadViewState(Object savedState) {
if (savedState != null) {
var pageState = (IDictionary<string, object>)savedState;
if (pageState.Contains("base")) {
base.LoadViewState(pageState["base"]);
}
// Iterate attributed properties. If pageState contains an
// item with the appropriate key, set the property value.
}
}
}继承自这个类的页面可以使用您提出的属性驱动语法。
发布于 2009-12-04 19:10:12
到目前为止,这就是我所得到的,TY Jeff为我指明了正确的方向:
TestPage:
public partial class Pages_Test : BasePage {
[ViewState]
public String Name { get; set; }BasePage:
#region Support ViewState Attribute
BindingFlags _flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
protected override Object SaveViewState()
{
object _baseState = base.SaveViewState();
IDictionary<string, object> _pageState = new Dictionary<string, object> { { "base", _baseState } };
//Use reflection to get properties marked for viewstate
foreach (PropertyInfo _property in GetType().GetProperties(_flags))
{
if (_property.HasAttribute<ViewState>())
{
object _value = _property.GetValue(this, _flags , null, null, null);
_pageState.Add(new KeyValuePair<string, object>(_property.Name, _value));
}
}
return _pageState;
}
protected override void LoadViewState(Object savedState)
{
if (savedState != null)
{
var _pageState = (IDictionary<string, object>)savedState;
if (_pageState.ContainsKey("base"))
{
base.LoadViewState(_pageState["base"]);
}
//use reflection to set properties
foreach (PropertyInfo _property in GetType().GetProperties(_flags ))
{
if (_property.HasAttribute<ViewState>() && _pageState.ContainsKey(_property.Name))
{
object _value = _pageState[_property.Name];
_property.SetValue(this, _value, _flags , null, null, null);
}
}
}
}
#endregion属性:
/// <summary>
/// This attribute is used by the BasePage to identify properties that should be persisted to ViewState
/// Note: Private properties are not supported
/// </summary>
[AttributeUsage(AttributeTargets.Property)]
public class ViewState : Attribute
{
//Marker
}帮助者:
public static class PropertyExtension
{
public static Boolean HasAttribute<T>(this PropertyInfo property)
{
object[] attrs = property.GetCustomAttributes(typeof(T), false);
return attrs != null && attrs.Length == 1;
}
}编辑
Jan有一个关于性能的有效观点,我做了一些分析,结果如下:
Without Attribute With Attribute Increase Slower %
One Property
First Load 0,004897899 0,010734255 0,005836356 219
Save, postback 0,002353861 0,010478008 0,008124147 445
Load, Postback 0,001488807 0,00627482 0,004786013 421
10 properties
First Load 0,006184096 0,015288675 0,009104579 247
Save, postback 0,004061759 0,015052262 0,010990503 371
Load, Postback 0,0015708 0,005833074 0,004262274 371
% increase
Avg Page. 0,902215714567075 0,00648在空页上增加是相当可观的,但在负载为1秒的平均页上,这种增加等于0.01%。
更新:使用PostSharp、PostSharp4ViewState
第一步:确保你的网站是预编译的
步骤2:安装PostSharp和PostSharp4ViewState
步骤3:参考PostSharp.Public和PostSharp4ViewState
步骤4:以下代码现在是有效的。
[Persist(Mode=PersistMode.ViewState)]
private string _name;
public String Name {
get { return _name; }
set { _name = value; }
}发布于 2009-12-04 19:16:03
由于大量使用反射,BBorg的解决方案实际上非常慢。
通过使用PostSharp.Laos,通过让您的属性继承自OnMethodBoundaryAspect,您可以轻松地覆盖public override void OnInvocation(MethodInvocationEventArgs eventArgs)并在其中执行所有的魔术。这样会快很多。例如,请查看PostSharp主页上的CacheAttribute示例。
如果你真的想要最快的速度,你可以编写一个PostSharp插件,将MSIL (GetFromViewState,SetInViewState方法或其他东西)编织到你的属性中,这样甚至不会有性能损失。
https://stackoverflow.com/questions/1673975
复制相似问题