如何解决有没有办法将参数或属性标记为可序列化?
我正在使用
- C# 2012
- Visual Basic 2012 (VB 11)
- ASP.NET 4.5
- Visual Studio 2012
Public Class State
private _state as StateBag
Public Property MyProp() as IEnumerable(Of MyType)
Get
Return getState("MyProp")
End Get
Set(value as IEnumerable(Of MyType))
setState("MyProp",value)
End Set
End Property
Private Function getState(key as String) as Object
Return Me._state(key)
End Function
Private Sub setState(key as String,value as Object)
Me._state(key) = value
End Sub
End Class
Public Class MyType
Public Property Id As UInt64
Public Property Value As String
End Class
最后在我的页面的代码隐藏中,我获取数据,在 GridView 上设置 DataSource,调用 DataBind(),最后我将数据存储在 ViewState 中
Protected Sub drawGrid()
Dim data As IEnumerable(MyType) = getData()
MyGridView.DataSource = data
MyGridView.DataBind()
Me.State.MyProp = data
End Sub
如果 MyType
类没有用 <Serializable()>
属性标记,GridView 不会显示任何数据。
所以我想知道是否有任何方法可以使我在尝试设置需要Serializable的属性时出现编译器错误?>
举个例子
<Serializable()> Public Property MyProp...
OR
Set (<Serializable()> value as IEnumerable(MyType))...
如果这是可能的,下次我忘记用“Serializable”标记一个类时,编译器会让我知道,而不是我盯着屏幕想:“为什么没有显示任何数据?? ?”
这是 C# 中的相同代码,但存在相同问题
-
MyPage.aspx
<%@ Page Title="MyPage" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="MyPage.aspx.cs" Inherits="WebApplication1.MyPage" %> <asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent"> <h1>MyPage should show some MyType</h1> <asp:GridView ID="MyGridView" runat="server" DataKeyNames="Id" AutoGenerateColumns="false" EmptyDataText="No MyType Data" Caption="MyType Grid" > <Columns> <asp:BoundField datafield="Id" HeaderText="Id" /> <asp:BoundField datafield="Name" HeaderText="Name" /> </Columns> </asp:GridView> </asp:Content>
-
MyPage.aspx.cs
using System; using System.Collections.Generic; namespace WebApplication1 { public class State { private System.Web.UI.StateBag _state; public IEnumerable<MyType> MyProp { get { return (IEnumerable<MyType>)this.getState("MyProp"); } set { this.setState("MyProp",value); } } private Object getState(String key) { return this._state[key]; } private void setState(String key,Object value) { this._state[key] = value; } public State(System.Web.UI.StateBag state) { this._state = state; } } [Serializable()] // This attribute MUST be here in order to save to the ViewState public class MyType { public UInt64 Id { get; set; } public String Name { get; set; } } public partial class MyPage : System.Web.UI.Page { private State _state; protected void Page_Load(object sender,EventArgs e) { this._state = new State(this.ViewState); loadData(); } protected void loadData() { IEnumerable<MyType> data = getData(); MyGridView.DataSource = data; MyGridView.DataBind(); this._state.MyProp = data; } private IEnumerable<MyType> getData() { var result = new List<MyType>(); for (UInt64 i = 1; i < 11; i++) { result.Add( new MyType { Id = i,Name = Guid.NewGuid().ToString("N") } ); } return result; } } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。