For example, in EF I define an entity like this:
[CSharp]View PlainCopyprint?
- [Table (Name = "Mytablename")]
- Public class MyClass
- {
- }
[Table (Name = "Mytablename")] public class MyClass { }
Now I want to get mytablename, can do this:
[CSharp]View PlainCopyprint?
- Using System.Data.Linq.Mapping;
- Namespace Myef
- {
- Class Program
- {
- static void Main (string[] args)
- {
- string name = typeof (MyClass). Getattributevalue (Tableattribute ta) = Ta. 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 = "Mytablename")]
- public class MyClass
- {
- }
- }
Gets the value of Tableattribute in EF, which is the real table name in the database