8,497
社区成员
发帖
与我相关
我的任务
分享
public class A_Abc
[Table("A_Abc")]
public class Abc
using System.Data.Linq.Mapping;
class Program
{
static void Main(string[] args)
{
string name = typeof(Abc).GetAttributeValue((TableAttribute dna) => dna.Name);
Console.WriteLine(name);
Console.Read();
}
}
public static class AttributeExtensions
{
public static TValue GetAttributeValue<TAttribute, TValue>(
this Type type,
Func<TAttribute, TValue> valueSelector)
where TAttribute : Attribute
{
var att = type.GetCustomAttributes(
typeof(TAttribute), true
).FirstOrDefault() as TAttribute;
if (att != null)
{
return valueSelector(att);
}
return default(TValue);
}
}
[Table(Name = "MyAbc")]
public class Abc
{
}