The text below is selected, press Ctrl+C to copy to your clipboard. (⌘+C on Mac) No line numbers will be copied.
Guest
Test 2
By Guest on 7th April 2017 10:00:55 AM | Syntax: TEXT | Views: 47



New paste | Download | Show/Hide line no. | Copy text to clipboard
  1. public static class Extensions
  2. {
  3.  //Metoda do pobierania naszego atrybutu z klas.
  4.  public static CarSegmentAttribute GetCarSegmentAttribute(this T obj) where T : class
  5.  {
  6.   var type = obj.GetType();
  7.   var customAttributes = type.GetCustomAttributes(typeof(CarSegmentAttribute), true);
  8.   if (customAttributes.Length > 0)
  9.    return customAttributes.First() as CarSegmentAttribute;
  10.   else
  11.    return null;
  12.  }
  13.  //Dla enumów metoda będzie wyglądała ciut inaczej.
  14.  public static CarSegmentAttribute GetCarSegmentAttributeFromEnum(this T obj) where T : struct, IConvertible
  15.   {
  16.    if (!typeof(T).IsEnum)
  17.      return null;
  18.  
  19.    MemberInfo memberInfo = obj.GetType().GetMember(obj.ToString()).FirstOrDefault();
  20.    if (memberInfo != null)
  21.     {
  22.      var customAttributes = memberInfo.GetCustomAttributes(typeof(CarSegmentAttribute), true);
  23.      if (customAttributes.Length > 0)
  24.        return customAttributes.First() as CarSegmentAttribute; // może być więcej niż jeden, ale my dla przykładu zwracamy zawsze jeden.
  25.       else
  26.        return null;
  27.   }
  28.  
  29.  return null;
  30.  }
  31. }



  • Recent Pastes