The Common Toolbox contains helper classes that can be useful in most ASP.NET 5 projects.
- Installation
- Components
- Handlers
- Helpers
- ExceptionHelper
- ReflectionHelper
- StringHelper
- StringHelper.GetValidString(string input)
- StringHelper.GetValidString(object input)
- StringHelper.RemoveWhitespaces(string input)
- StringHelper.TrimOrDefault(string input)
- StringHelper.ToCamelCase(string input)
- StringHelper.ToPascalCase(string input)
- StringHelper.FromBase64(string input)
- StringHelper.ToBase64(string input)
- UriHelper
- Validation
- Version History
The toolbox is added to a project via the NuGet Package Manager in Visual Studio or by adding the package directly to the project.json file :
"dependencies": {
"Digipolis.Common": "2.0.0",
}
## Components
Inherit from this class if you have a class that must implement IDisposable. In the inherited class you can implement the dispose logic by overriding the methods DisposeManagedResources and DisposeUnmanagedResources.
## Handlers
The PlatformHandler wraps the PlatformID in a class and adds methods tho easily check which platform the source code is running on. It can be used in a loosely coupled project where the PlatformID can be mocked for testing purposes.
The methods and platformID are available through the Platform property. By default the Platform property is initialized to the OS of the machine. The Platform can be changed (e.g. for unit tests) through its setter.
PlatformHandler.Platform = new Platform(PlatformID.MacOSX);
Returns true if the platform is Linux.
Returns true if the platform is Mac OSX.
Returns true if the platform is Unix.
Returns true if the platform is Windows.
Returns true if the platform is XBox.
## Helpers
The ExceptionHelper has methods to recursively extract properties of (inner-)exceptions. This can be handy when logging errors.
#### ExceptionHelper.GetAllMessages() Returns a string with all messages of all (inner-)exceptions.
Returns a string with all stacktrace of all (inner-)exceptions.
Returns a string with the result of all ToString() calls of all (inner-)exceptions.
### ReflectionHelper
The ReflectionHelper provides methods that wrap common used reflection scenario's.
Returns the attribute if defined on the given type, null if the type does not have the attribute.
var attrib = ReflectionHelper.GetAttributeFrom<AnAttribute>(typeof(AClass));
Returns a list of classes that inherit from the given class.
var classes = ReflectionHelper.GetClassesInheritingFrom(Assembly.GetCallingAssembly(), typeof(BaseClass));
Returns a list of assemblies that reference the given assembly.
var assemblies = ReflectionHelper.GetReferencingAssemblies("Digipolis.Common.dll");
Returns a list of the given types in the AppDomain.
var types = ReflectionHelper.GetTypesFromAppDomain("MyClass");
Returns a list of the types that start with the given string.
var types = ReflectionHelper.GetTypesThatStartWith("Http");
Returns a list of the types that are decorated with the given attribute.
var types = ReflectionHelper.GetTypesWithAttribute<AnAttribute>(Assembly.GetExecutingAssembly(), true);
### StringHelper
Returns a string value that contains the contents of the given string or specifies whether the string is empty or null.
Returns the ToString() value of the given object or null if the object is null.
The input string without whitespace characters, null if the input string is null.
Returns null (= default) if the input value is null and the trimmed value if they are not null.
Returns the given string, starting with a lowercase letter.
Returns the given string, beginning with an uppercase letter.
Returns a decoded string of the base64 input string.
Returns the given string in base64 format.
### UriHelper
Returns true if the given string contains a valid url.
Returns the name of the primary host.
The ArgumentValidator can be used to check arguments that are used as parameters in e.g. a method. It provides a way to enforce code contracts at run time.
Raises an ArgumentException if the given string is an empty string.
Raises an ArgumentNullException if the given string or object is null.
Raises an ArgumentNullException if the given string is null or an ArgumentException if the given string is an empty string.
Raises an ArgumentNullException if the given string is null or an ArgumentException if the given string is an empty string or contains only white spaces.
Usage :
(e.g. Code contract : builder must be an instantiated object and route must contain a value)
public void SetRoute(IApplicationBuilder builder, string route)
{
ArgumentValidator.AssertNotNull(builder, nameof(builder));
ArgumentValidator.AssertNotNullOrWhiteSpace(route, nameof(route));
// Set the route, using the builder
}
The content of the exception messages can be set via the Message property :
ArgumentValidator.Messages.NullString = "My own null-string error message.";
ArgumentValidator.Messages.EmptyString = "My own empty-string error message with {0} parameter.";
The default message values can be restored with the SetDefaults method :
ArgumentValidator.Messages.SetDefaults();
The default messages are :
Property | Message |
---|---|
NullString | {0} is null. |
EmptyString | {0} is empty. |
WhiteSpaceString | {0} contains only spaces. |
Version | Date | Author | Description |
---|---|---|---|
1.0.1 | 2015-09-08 | Steven Vanden Broeck | Init. |
1.0.2 | 2015-09-09 | Steven Vanden Broeck | ReflectionHelper. |
1.0.3 | 2015-09-24 | Steven Vanden Broeck | PlatformHandler. |
1.0.4 | 2015-09-25 | Steven Vanden Broeck | PlatformHandler extended. |
1.0.5 | 2015-10-15 | Steven Vanden Broeck | ExceptionHelper. |
1.1.0 | 2015-12-28 | Steven Vanden Broeck | Upgrade to ASP.NET 5 RC1. |
1.2.1 | 2016-02-17 | Koen Stroobants | StringHelper, UriHelper, DisposableObject, DateTimeComparer. |