您的位置:首页 > 其它

Silverlight实战示例2(兼集合属性的妙用)--实体的组织

2011-08-24 21:52 309 查看
在本篇中,我们不仅演示实体的结构,而且我们利用集合属性来打造万能实体(类似于DataTable)。 下面是代码:

1)首先我们定义Column,主要提供字段列信息:DynamicDataColumn.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace MEntities

{

[Serializable]

public partial class DynamicDataField

{

public string FieldName { get; set; }

public string StrValue { get; set; }

public DateTime DTValue { get; set; }

public Byte[] ByteArrayValue { get; set; }

public string DataType { get; set; }

}

}

2)定义数据字段,用于字段数据的存储,如果数据类型都可以整成字符串的话,其实可以简化这块,而是参照我前面的文章中提到的,利用字典集合来做,但这里提供的方式会更专业一些:DynamicDataField.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace MEntities

{

[Serializable]

public partial class DynamicDataField

{

public string FieldName { get; set; }

public string StrValue { get; set; }

public DateTime DTValue { get; set; }

public Byte[] ByteArrayValue { get; set; }

public string DataType { get; set; }

}

}

DynamicDataField.Shared.cs:因为有些辅助方法无法自动生成,可通过代码共享来达到索引器穿越的目的

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace MEntities

{

public partial class DynamicDataField

{

public object Value

{

get

{

if (this.DataType == "datatime")

{

return this.DTValue;

}

if (this.DataType == "byte[]")

{

return this.ByteArrayValue;

}

return this.StrValue;

}

set

{

if (this.DataType == "datatime")

{

DTValue = (DateTime) value;

}

if (this.DataType == "byte[]")

{

this.ByteArrayValue =(byte[])value;

}

this.StrValue = value.ToString();

}

}

}

}

因为数据类型穿越没什么实际意义,所以这里用字符串来表达数据类型,而没有用Type类型。因为数据类型可以是数据库的数据类型,用字符串表达更为自由。

待续.....
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐