问题描述
我有以下类似的类,这些类用作我的一张桌子的实体框架核心模型。 保存后,我将对象传递给某种缓存工作。 如何获取TableName和标记为Key的字段的值(我将单个字段用作Key)
[Table("audit_log")]
public class AuditLog
{
[Key]
public long auditlog_id { get; set; }
[required]
public string appname { get; set; }
}
TableName来自System.ComponentModel.DataAnnotations.Schema 密钥来自System.ComponentModel.DataAnnotations
我使用了自己的自定义属性,但是在这里想检查是否存在一些访问这些值的内置方法。 我必须传递对象并获取键的表名和值
解决方法
根据您的需要调整此测试代码:
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.IO;
using System.Linq;
using System.Reflection;
class Program
{
[Table("audit_log")]
public class AuditLog
{
[Key] public long auditlog_id { get; set; }
[Required]
public string appname { get; set; }
}
static void Main(string[] args)
{
GetClassAttributes(typeof(AuditLog ));
}
public static void GetClassAttributes(System.Type item)
{
var tableName=string.Empty;
var tableAttr=item.GetCustomAttributes().FirstOrDefault() as TableAttribute;
if (tableAttr != null) tableName = tableAttr.Name;
Console.WriteLine("");
Console.WriteLine("Table Name .... {0}",tableName);
var properties = item.GetProperties();
if (HasEFDataAnnotaion(properties))
{
Console.WriteLine("");
Console.WriteLine("Found Data Annotations attributes at {0} ...",item.FullName);
foreach (var property in properties)
{
var attributes = property.GetCustomAttributes(false);
// Using reflection.
Attribute[] attrs = System.Attribute.GetCustomAttributes(property);
// Displaying output.
foreach (Attribute attr in attrs)
{
if (attr is KeyAttribute)
{
KeyAttribute a = (KeyAttribute)attr;
Console.WriteLine("attribute {0} on {1} ",a.ToString(),property.Name);
}
if (attr is ForeignKeyAttribute)
{
ForeignKeyAttribute a = (ForeignKeyAttribute)attr;
Console.WriteLine("attribute {0} on {1} ",property.Name);
}
//if (attr is IndexAttribute)
//{
// IndexAttribute a = (IndexAttribute)attr;
// Console.WriteLine("attribute {0} on {1} ",a.GetType().FullName + a.ToString(),property.Name);
//}
if (attr is RequiredAttribute)
{
RequiredAttribute a = (RequiredAttribute)attr;
Console.WriteLine("attribute {0} on {1} ",property.Name);
}
if (attr is TimestampAttribute)
{
TimestampAttribute a = (TimestampAttribute)attr;
Console.WriteLine("attribute {0} on {1} ",property.Name);
}
if (attr is ConcurrencyCheckAttribute)
{
ConcurrencyCheckAttribute a = (ConcurrencyCheckAttribute)attr;
Console.WriteLine("attribute {0} on {1} ",property.Name);
}
if (attr is MinLengthAttribute)
{
MinLengthAttribute a = (MinLengthAttribute)attr;
Console.WriteLine("attribute {0} on {1} ",property.Name);
}
if (attr is MaxLengthAttribute)
{
MaxLengthAttribute a = (MaxLengthAttribute)attr;
Console.WriteLine("attribute {0} on {1} ",property.Name);
}
if (attr is StringLengthAttribute)
{
StringLengthAttribute a = (StringLengthAttribute)attr;
Console.WriteLine("attribute {0} on {1} ",property.Name);
}
if (attr is TableAttribute)
{
TableAttribute a = (TableAttribute)attr;
Console.WriteLine("attribute {0} on {1} ",property.Name);
}
if (attr is ColumnAttribute)
{
ColumnAttribute a = (ColumnAttribute)attr;
Console.WriteLine("attribute {0} on {1} ",property.Name);
}
if (attr is DatabaseGeneratedAttribute)
{
DatabaseGeneratedAttribute a = (DatabaseGeneratedAttribute)attr;
Console.WriteLine("attribute {0} on {1} ",property.Name);
}
if (attr is ComplexTypeAttribute)
{
ComplexTypeAttribute a = (ComplexTypeAttribute)attr;
Console.WriteLine("attribute {0} on {1} ",property.Name);
}
}
}
}
}
}
private static bool HasEFDataAnnotaion(PropertyInfo[] properties)
{
return properties.ToList().Any((property) =>
{
var attributes = property.GetCustomAttributes(false);
Attribute[] attrs = System.Attribute.GetCustomAttributes(property);
return attrs.Any((attr) =>
{
return attr is KeyAttribute || attr is ForeignKeyAttribute || attr is RequiredAttribute || attr is TimestampAttribute //|| attr is IndexAttribute
|| attr is ConcurrencyCheckAttribute || attr is MinLengthAttribute || attr is MinLengthAttribute
|| attr is MaxLengthAttribute || attr is StringLengthAttribute || attr is TableAttribute || attr is ColumnAttribute
|| attr is DatabaseGeneratedAttribute || attr is ComplexTypeAttribute;
});
});
}
更新
如果您需要键属性值,请将我的代码开头更改为:
static void Main(string[] args)
{
var auditLog = new AuditLog {auditlog_id=1,appname="first log"};
GetObjectAttributes(auditLog);
}
public static void GetObjectAttributes(object item)
{
var itemType= item.GetType() ;
var tableName=string.Empty;
var tableAttr=itemType.GetCustomAttributes().FirstOrDefault() as TableAttribute;
if (tableAttr != null) tableName = tableAttr.Name;
Console.WriteLine("");
Console.WriteLine("Table Name .... {0}",tableName);
var properties = itemType.GetProperties();
if (HasEFDataAnnotaion(properties))
{
Console.WriteLine("");
Console.WriteLine("Found Data Annotations attributes at {0} ...",tableName);
foreach (var property in properties)
{
var attributes = property.GetCustomAttributes(false);
// Using reflection.
Attribute[] attrs = System.Attribute.GetCustomAttributes(property);
// Displaying output.
foreach (Attribute attr in attrs)
{
if (attr is KeyAttribute)
{
KeyAttribute a = (KeyAttribute)attr;
Console.WriteLine("attribute {0} on {1} ",property.Name);
Console.WriteLine("property value on {1} is {0} ",property.GetValue(item ),property.Name);
}
......Continue code above if you need another attributes
,
非常感谢Serge。 以下是我目前的解决方法
public static Tuple<bool,string,string> GetDataAnotationDetail<T>(T item) where T : class
{
Tuple<bool,string> result = null;
var tableName = string.Empty;
var tableAttr = item.GetType().GetCustomAttributes().FirstOrDefault() as TableAttribute;
if (tableAttr != null)
{
tableName = tableAttr.Name;
}
var properties = item.GetType().GetProperties();
bool breakcondition = false;
foreach (var property in properties)
{
Attribute[] attrs = Attribute.GetCustomAttributes(property);
if (attrs != null)
{
foreach (Attribute attr in attrs)
{
if (attr is KeyAttribute)
{
var a = (KeyAttribute)attr;
var obj = property.GetValue(item,null);
result = Tuple.Create(true,tableName,Convert.ToString(obj));
breakcondition = true;
break;
}
}
}
if (breakcondition)
{
break;
}
}
return result;
}