时间:2016-02-15 22:24 来源: 我爱IT技术网 作者:佚名
欢迎您访问我爱IT技术网,今天小编为你分享的编程技术是:【.net使用自定义类属性实例】,下面是详细的讲解!
.net使用自定义类属性实例
public class BaseEntity{
public BaseEntity()
{
}
/// <summary>
/// 获取表名称
/// </summary>
/// <returns></returns>
public string GetTableName()
{
Type type=this.GetType();
object[] objs=type.GetCustomAttributes(typeof(TableAttribute), true);
if (objs.Length <=0)
{
throw new Exception("实体类没有标识TableAttribute属性");
}
else
{
object obj=objs[0];
TableAttribute ta=(TableAttribute)obj;
return ta.TableName; //获取表名称
}
}
/// <summary>
/// 获取数据实体类上的FieldAttribute
/// </summary>
/// <param name="propertyName"></param>
/// <returns></returns>
public FieldAttribute GetFieldAttribute(string propertyName)
{
PropertyInfo field=this.GetType().GetProperty(propertyName);
if (field==null)
{
throw new Exception("属性名" + propertyName + "不存在");
}
object[] objs=field.GetCustomAttributes(typeof(FieldAttribute), true);
if (objs.Length <=0)
{
throw new Exception("类体属性名" + propertyName + "没有标识FieldAttribute属性");
}
else
{
object obj=objs[0];
FieldAttribute fieldAttribute=(FieldAttribute)obj;
fieldAttribute.FieldValue=field.GetValue(this,null);
return fieldAttribute;
}
}
}
关于.net使用自定义类属性实例的用户互动如下:
相关问题:
答: >>详细
相关问题:
答: >>详细
相关问题:
答: >>详细
- 【asp】asp.net url重写浅谈-net-url重写
- 【DataSet】DataSet、DataTable、DataRow区别详解
- 【asp】asp.net 动态添加多个用户控件-net-动态添
- 【创建】ASP.NET Web API教程 创建域模型的方法详
- 【asp】asp.net ubb使用代码-net-ubb使用
- 【默认图片】图片不存在使用默认图片代替的实例
- 【asp】asp.net 页面转向 Response.Redirect Ser
- 【页面打印】关于ASP.NET页面打印技术的常用方法
- 【MVC5】MVC 5 第一章 创建MVC 5 web应用程序-net
- 【MVC】一个简单MVC5 + EF6示例分享-EF6实例-MVC5
- 评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
-
