标签:实例
在前一篇文章中介绍到了Attribute的一些基本概念,至于它的一些具体的用法还没有意义解释,现在就针对Attribute的自定义问题,通过实例来加强了解吧!
<span style="font-size:18px;">using System;
usingSystem.Collections.Generic;
usingSystem.Diagnostics;
using System.Linq;
usingSystem.Runtime.InteropServices;
using System.Text;
usingSystem.Reflection;
using System.Data;
namespaceConsoleApplication1
{
#region ORMapping
[AttributeUsageAttribute(AttributeTargets.Class, Inherited = false,AllowMultiple = false)]
public class TableAttribute : Attribute
{
//保存表名的字段
private string _tableName;
public TableAttribute()
{
}
public TableAttribute(string tableName)
{
this._tableName = tableName;
}
/// <summary>
/// 映射的表名(表的全名:模式名.表名)
/// </summary>
public string TableName
{
set
{
this._tableName = value;
}
get
{
return this._tableName;
}
}
}
[AttributeUsageAttribute(AttributeTargets.Property, Inherited = false,AllowMultiple = false)]
public class ColumAttribute : Attribute
{
private string _columName;
private DbType _dbType;
public ColumAttribute()
{
}
public ColumAttribute(string columName)
: this()
{
this._columName = columName;
}
public ColumAttribute(string columName,DbType dbType)
: this(columName)
{
this._columName = columName;
this._dbType = dbType;
}
//列名
public virtual string ColumName
{
set
{
this._columName = value;
}
get
{
return this._columName;
}
}
//描述一些特殊的数据库类型
public DbType DbType
{
get { return _dbType; }
set { _dbType = value; }
}
}</span><span style="font-size:18px;"> [Table("User")]
public class User
{
[Colum("userID", DbType =DbType.Int32)]
public int UserID { get; set; }
[Colum("UserName", DbType =DbType.String)]
public string UserName { get; set; }
}</span><span style="font-size:18px;"> public class ORMHelp
{
public void Insert(object table)
{
Type type = table.GetType();
//定义一个字典来存放表中字段和值的对应序列
Dictionary<string, string>columValue = new Dictionary<string, string>();
//用于拼接sql字符串
StringBuilder SqlStr = newStringBuilder();
SqlStr.Append("insert into");
//得到表名子
TableAttribute temp =(TableAttribute)type.GetCustomAttributes(typeof(TableAttribute),false).FirstOrDefault();
SqlStr.Append(temp.TableName);
SqlStr.Append("(");
PropertyInfo[] Propertys =type.GetProperties();
foreach (var item in Propertys)
{
object[] attributes =item.GetCustomAttributes(false);
foreach (var item1 inattributes)
{
//获得相应属性的值
string value =table.GetType().InvokeMember(item.Name,System.Reflection.BindingFlags.GetProperty, null, table, null).ToString();
ColumAttribute colum =item1 as ColumAttribute;
if (colum != null)
{
columValue.Add(colum.ColumName, value);
}
}
}
//拼插入操作字符串
foreach (var item in columValue)
{
SqlStr.Append(item.Key);
SqlStr.Append(",");
}
SqlStr.Remove(SqlStr.Length - 1,1);
SqlStr.Append(")values('");
foreach (var item in columValue)
{
SqlStr.Append(item.Value);
SqlStr.Append("','");
}
SqlStr.Remove(SqlStr.Length - 2,2);
SqlStr.Append(")");
Console.WriteLine(SqlStr.ToString());
Console.Read();
}
}</span><span style="font-size:18px;"> public class project
{
//实例ORMapping
static void Main(string[] args)
{
ORMHelp o = new ORMHelp();
User u = new User() { UserID = 1,UserName = "lfm" };
o.Insert(u);
}
}</span> 执行后的效果:
Attribute的自定义简单的来说就 是一种实例化方式比较特殊的类,特别注意的就是AttributeUsage 这个专门用来修饰Attribute的Attribute ,除了可以控制修饰目标外,还能决定被它修饰的Attribute是否可以随宿主“遗传”,以及是否可以使用多个实例来修饰同一个目标!希望在今后的学习和工作中经常用到。
标签:实例
原文地址:http://blog.csdn.net/liu_yujie2011com/article/details/43709387