Cycle de progression Faire (quelque chose qui marche) -> comprendre ce que l’on fait/comment cela marche -> pousser plus loin les notions
| [AttributeUsage(AttributeTargets.Property,AllowMultiple=false)] public class MappingAttribute : System.Attribute { private string _Storage; public string Storage { get { return _Storage; } set { _Storage = value; } } private string _Name; public string Name { get { return _Name; } set { _Name = value; } } private bool _isPrimaryKey; public bool IsPrimaryKey { get { return _isPrimaryKey; } set { _isPrimaryKey = value; } } private bool _CanBeNull; public bool CanBeNull { get { return _CanBeNull; } set { _CanBeNull = value; } } private string _DBType; public string DBType { get { return _DBType; } set { _DBType = value; } } public MappingAttribute() { } public MappingAttribute(string Storage, string Name, bool IsPrimaryKey, bool CanBeNull, string DBType) { this.Storage = Storage; this.Name = Name; this.IsPrimaryKey = IsPrimaryKey; this.CanBeNull = CanBeNull; this.DBType = DBType; } |
| public class Contact { private string _ContactName; [MappingAttribute(Storage = "ContactName", Name = "Name", DBType = "CHAR(150)", IsPrimaryKey = false, CanBeNull = false)] public string ContactName { get { return _ContactName; } set { _ContactName = value; } } public Contact() { } public Contact(string ContactName) { this.ContactName = ContactName; } } |
| private void button1_Click(object sender, EventArgs e) { Contact oContact = new Contact("Romagny"); MessageBox.Show(GetMappingAttributeOfProperty("ContactName").Name); } public MappingAttribute GetMappingAttributeOfProperty(string PropertyName) { MappingAttribute oMappingAttribute = null; System.Reflection.Assembly oAssembly = System.Reflection.Assembly.GetExecutingAssembly(); foreach(Type oType in oAssembly.GetTypes()) { if (oType.Name == "Contact") { foreach (System.Reflection.PropertyInfo oPropertyInfo in oType.GetProperties()) { if (oPropertyInfo.Name == PropertyName) { object[] CustomAttributes = oPropertyInfo.GetCustomAttributes(typeof(MappingAttribute), true); oMappingAttribute = CustomAttributes[0] as MappingAttribute; } } } } return oMappingAttribute; } |
| public MappingAttribute GetMappingAttributeOfProperty(string PropertyName) { System.Reflection.Assembly oAssembly = System.Reflection.Assembly.GetExecutingAssembly(); return oAssembly.GetType("AttributsPersonnalises.Contact").GetProperty(PropertyName).GetCustomAttributes(typeof(MappingAttribute), true)[0] as MappingAttribute; } |