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: 74



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
  • Gfdg
    2 days 16 hours 31 mins 17 secs ago
  • LOL
    1 week 13 hours 29 mins and 16 secs ago
  • Wert
    1 week 1 day 8 hours 24 mins and 36 secs ago
  • 354643643
    1 week 6 days 2 hours 19 mins and 18 secs ago
  • Qwerty
    2 weeks 1 hour 49 mins and 25 secs ago