para imagen emplea atributo c# .net custom-attributes

c# - imagen - atributo title html5



Atributo personalizado en propiedad-Obtención de tipo y valor de propiedad atribuida (3)

Tengo el siguiente atributo personalizado, que se puede aplicar en las propiedades:

[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)] public class IdentifierAttribute : Attribute { }

Por ejemplo:

public class MyClass { [Identifier()] public string Name { get; set; } public int SomeNumber { get; set; } public string SomeOtherProperty { get; set; } }

También habrá otras clases, a las que el atributo identificador podría agregarse a propiedades de diferente tipo:

public class MyOtherClass { public string Name { get; set; } [Identifier()] public int SomeNumber { get; set; } public string SomeOtherProperty { get; set; } }

Entonces necesito poder obtener esta información en mi clase consumidora. Por ejemplo:

public class TestClass<T> { public void GetIDForPassedInObject(T obj) { var type = obj.GetType(); //type.GetCustomAttributes(true)??? } }

¿Cuál es la mejor manera de hacer esto? Necesito obtener el tipo del campo [Identificador ()] (int, cadena, etc.) y el valor real, obviamente basado en el tipo.


Algo como lo siguiente, esto usará solo la primera propiedad con la que se encuentre que tenga el atributo, por supuesto, podría colocarlo en más de una.

public object GetIDForPassedInObject(T obj) { var prop = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance) .FirstOrDefault(p => p.GetCustomAttributes(typeof(IdentifierAttribute), false).Count() ==1); object ret = prop !=null ? prop.GetValue(obj, null) : null; return ret; }


Un poco tarde, pero aquí hay algo que hice para enums (también podría ser cualquier objeto) y obtener el valor del atributo de descripción con una extensión (esto podría ser un genérico para cualquier atributo):

public enum TransactionTypeEnum { [Description("Text here!")] DROP = 1, [Description("More text here!")] PICKUP = 2, ... }

Obteniendo el valor:

var code = TransactionTypeEnum.DROP.ToCode();

Extensión que soporta todos mis enums:

public static string ToCode(this TransactionTypeEnum val) { return GetCode(val); } public static string ToCode(this DockStatusEnum val) { return GetCode(val); } public static string ToCode(this TrailerStatusEnum val) { return GetCode(val); } public static string ToCode(this DockTrailerStatusEnum val) { return GetCode(val); } public static string ToCode(this EncodingType val) { return GetCode(val); } private static string GetCode(object val) { var attributes = (DescriptionAttribute[])val.GetType().GetField(val.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), false); return attributes.Length > 0 ? attributes[0].Description : string.Empty; }


public class TestClass<T> { public void GetIDForPassedInObject(T obj) { PropertyInfo[] properties = obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance); PropertyInfo IdProperty = (from PropertyInfo property in properties where property.GetCustomAttributes(typeof(Identifier), true).Length > 0 select property).First(); if(null == IdProperty) throw new ArgumentException("obj does not have Identifier."); Object propValue = IdProperty.GetValue(entity, null) } }