在本篇中,我们不仅演示实体的结构,而且我们利用集合属性来打造万能实体(类似于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类型。因为数据类型可以是数据库的数据类型,用字符串表达更为自由。
待续.....
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。