Nun, das ist, was ich habe, so weit, TY Jeff für mich in die richtige Richtung:
Testseite:
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
Attribut:
/// <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
}
Helfer:
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;
}
}
EDIT
Jan einen wichtigen Punkt über die Leistung hat, habe ich einige der Profilierung mit den folgenden Ergebnissen:
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
Auf einer leeren Seite ist der Anstieg beträchtlich, aber auf einer durchschnittlichen Seite mit einer Last von 1s beträgt diese Zunahme 0,01%.
aktualisieren: Mit Postsharp, PostSharp4ViewState
Schritt 1: Stellen Sie sicher, dass Ihre Website
Schritt 2 vorkompilierte ist: Installieren Sie PostSharp und PostSharp4ViewState
Schritt 3: Referenz PostSharp.Public Und PostSharp4ViewState
Schritt 4: Im Anschluss ist Code jetzt gültig.
[Persist(Mode=PersistMode.ViewState)]
private string _name;
public String Name {
get { return _name; }
set { _name = value; }
}
Ich denke nicht, aber ich werde davon absehen, dies zu einer Antwort zu machen, weil ich die aspektorientierten Programmierer gesehen habe, die ab und zu einige erstaunliche Dinge mit Attributen machen. –
Klingt nach etwas, das PostSharp für Sie tun kann. – sisve