A set of extensions methods over standard .Net types
A set of extensions methods over IDictionary<TKey, TValue>
DictionaryExtensions.GetValueOrDefault<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key)
Gets the value if it exists in the dictionary or the default of TValue.
Dictionary<string,int>d=newDictionary<string,int>();intvalue=d.GetValueOrDefault("some key");DictionaryExtensions.GetValueOrNew<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key)
Gets the value if it exists in the dictionary or it creates a new instance which is added and returned.
Dictionary<string,MyType>d=newDictionary<string,MyType>();MyTypeactual=d.GetValueOrNew("some key");DictionaryExtensions.GetValueOrNew<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, Func<TValue> factory)
Gets the value if it exists in the dictionary or it creates a new instance using the specified factory which is added and returned.
Dictionary<string,string>d=newDictionary<string,string>();stringactual=d.GetValueOrNew("some key",()=>"some value");DictionaryExtensions.ToNameValueCollection<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, Func<TValue, string> convertFunc = null)
Converts the dictionary to a name value collection. The keys are converted to string using ToString() function. By default ToString() is used to convert a value, if a conversion function is not provided
Dictionary<int,string>d=newDictionary<int,string>{{1,"Value1"},{2,"Value2"}};NameValueCollectionc=d.ToNameValueCollection();A set of extensions methods over IEnumerable<T>
EnumerableExtensions.ReplaceAll<T>(this IEnumerable<T> collection, T source, T replacement)
Replaces all occurrences of source element in given IEnumerable with replacement element. If more occurrences are found all are replaced, with ONE replacement. Therefore, the resulted IEnumerable has less elements. Default EqualityComparer is used to determine equality. Null element is handled as any other element
char[]chars={'a','b','c'};IEnumerable<char>actual=chars.ReplaceAll('b','x');EnumerableExtensions.ReplaceAll<T>(this IEnumerable<T> collection, T source, T replacement, IEqualityComparer<T> comparer)
Replaces all occurrences of source element in given IEnumerable with replacement element. If more occurrences are found all are replaced, with ONE replacement. Therefore, the resulted IEnumerable has less elements. The specified IEqualityComparer is used to determine equality. Null element is handled as any other element
char[]chars={'a','b','c'};IEqualityComparer<char>comparer=newCustomEqualityComparer();IEnumerable<char>actual=chars.ReplaceAll('b','x',comparer);EnumerableExtensions.Replace<T>(this IEnumerable<T> collection, T source, T replacement)
Replaces each occurrences of source element in given IEnumerable with replacement element. If more occurrences are found each one is replaced, resulting same number of elements in output IEnumerable Default EqualityComparer is used to determine equality. Null element is handled as any other element
char[]chars={'a','b','c'};IEnumerable<char>actual=chars.Replace('b','x');EnumerableExtensions.Replace<T>(this IEnumerable<T> collection, T source, T replacement, IEqualityComparer<T> comparer)
Replaces each occurrences of source element in given IEnumerable with replacement element. If more occurrences are found each one is replaced, resulting same number of elements in output IEnumerable. The specified IEqualityComparer is used to determine equality. Null element is handled as any other element
char[]chars={'a','b','c'};IEqualityComparer<char>comparer=newCustomEqualityComparer();IEnumerable<char>actual=chars.Replace('b','x',comparer);EnumerableExtensions.ForEach<T>(this IEnumerable<T> collection, Action<T> action)
Executes the action on each element from the enumerable.
List<int>actual=newList<int>();int[]e={1,2};e.ForEach(x =>actual.Add(x));EnumerableExtensions.Includes<T>(this IEnumerable<T> superset, IEnumerable<T> subset)
Verifies if every element of the subset is also an element of the superset.
char[]superset=new[]{'a','b','c'};char[]subset=new[]{'b','c'};boolincluded=superset.Includes(subset);A set of extensions methods over Exception
ExceptionExtensions.FirstInner<T>(this Exception exception)
Gets the first inner exception of type T. Returns null if not found.
ArgumentNullExceptionane=newArgumentNullException();Exceptione=newException("",ane);ArgumentNullExceptionactual=e.FirstInner<ArgumentNullException>();ExceptionExtensions.InnerMostException(this Exception exception)
Gets the deepest inner exception.
ArgumentExceptionae=newArgumentException();ArgumentNullExceptionane=newArgumentNullException("",ae);Exceptione=newException("",ane);Exceptionactual=e.InnerMostException();Assert.Same(ae,actual);A set of extensions methods over String
String.MatchesWildcard(this string input, string wildcard)
Returns true if the input matches the wildcard pattern. It is case sensitive.
stringinput="BXgin_SomeText_End";boolmatch=input.MatchesWildcard("B?gin*End");A set of extensions methods over Type
Type.GetGenericInterface(this Type type, Type genericInterfaceTypeDefinition)
Gets the generic interface implemented by this type, which complies with given interface type definition. An interface type definition is an open generic type which is an interface.
Typeresult=typeof(GenericInterfaceImpl<int,string>).GetGenericInterface(typeof(IGenericInterface<,>));if(result!=null){// type implements IGenericInterface<,>}Type.GetGenericInterface(this Type type, Type genericInterfaceTypeDefinition)
Gets the type arguments of the generic interface implemented by this type. The generic interface should match the given interface type definition. An interface type definition is an open generic type which is an interface.
Type[]result=typeof(GenericInterfaceImpl<int,string>).GetGenericInterfaceArguments(typeof(IGenericInterface<,>));// result is [ typeof(int), typeof(string) ]ReflectionExtensions.GetAttribute<TAttribute>(object instance)
Gets the attribute of type TAttribute from the instance type. Returns null if the attribute was not found.
MyTypeinstance=newMyType();TableAttributeattribute=instance.GetAttribute<TableAttribute>();ReflectionExtensions.GetAttribute<TAttribute>(this ICustomAttributeProvider attributeProvider)
Gets the attribute of type TAttribute from the attribute provider. Returns null if the attribute was not found.
TableAttributetable=typeof(MyClass).GetAttribute<TableAttribute>();foreach(PropertyInfopropertyintypeof(MyClass).GetProperties()){ColumnAttributecolumn=property.GetAttribute<ColumnAttribute>();}ReflectionExtensions.GetAttributes<TAttribute>(this ICustomAttributeProvider attributeProvider)
Gets all the attributes of type TAttribute from the attribute provider.
IEnumerable<ServiceAttribute>services=typeof(MyClass).GetAttributes<ServiceAttribute>();PropertyInfoproperty=typeof(MyClass).GetProperty("Name");IEnumerable<ValidationAttribute>validations=property.GetAttributes<ValidationAttribute>();ReflectionExtensions.IsSimpleType(this Type type)
A type is simple if it is primitive or value type, or enum or string
IEnumerable<PropertyInfo>simpleProperties=typeof(MyClass).GetProperties().Where(p =>p.PropertyType.IsSimpleType())A set of extensions to order elements by their type priority
PriorityExtensions.OrderByPriority<T>(this IEnumerable<T> items)
Orders the elements by priority attribute on their type.
interfaceIPrio{}[Priority(1)]publicclassPrio1:IPrio{}[Priority(2)]publicclassPrio2:IPrio{}IPriop1=newPrio1();IPriop2=newPrio2();IPrio[]items=newIPrio[]{p1,p2};IPrio[]ordered=items.OrderByPriority();