Utilities class for .Net including Dynamic property getters/setters, automatic IQueryable searching, LinqToSql shortcuts, FileStream shortcuts, String extensions and more
This is a utility class that was developed with the help of others that I use in almost all of my .Net projects. It is available
via NuGet as well as on
CodePlex. It includes:
DynamicProperty
Change a property value at run time without knowing the property ahead of time:
someObject.SetPropertyValue("Name", "new value")
// is the same as
someObject.Name = "new value";
// no need to know which property to code in
More Information
IQueryableSearch
Search all/any properties of an IQueryable with one single search.
The 'quick search' is a great tool. Google has shown us that searching with one single, universal search field is the way that people prefer to search. Anyone who has worked with Linq for any amount of time knows this is possible, but it requires a lot of hard-coding and a long jumble of 'where' statements. This class will allow you to run a universal 'google-like' search on any IQueryable.
More Information
var results = cars.Search(new object[] {"chevy", 2007});
// will search cars for the string "chevy"
// and the int 2007 across all fields
LinqToSql
Universal Get Extensions for your DataContexts, Find the Primary Key of any table, and more
Naspinski.Utilities.GetPrimaryKey<table>();
// will return the PropertyInfo of the Primary Key(s) of 'table'
someDataContext.Get<Car>(someKey);
// is the same as writing:
someDataContext.Cars.FirstOrDefault(x => x.id == someKey);
// regardless of what Type someKey is or what the
// PropertyInfo.Name of the Primary Key is; never write
// a Get accessor again!
FileStreamSave
Simple extension to save a FileStream to disk, option to avoid overwriting will automatically update the filename to avoid overwriting:
someFileStream.Save(@"C:\file.txt")
// will save the file to the given path
// 'file[1].txt' if the file is already there
// the file name will be returned
someFileStream.Save(@"C:\file.txt", false);
// will save the file to the given path,
// overwriting if the file already exists
StringConversions
Convert strings to things you often need to convert them to, easily.
string s = s.RemoveCharacters(new[] { 'a', 'b' })
// removes any instances of 'a' or 'b' from s
string s = Strings.Random(10)
// random string, all alphanumeric, length 10
string s = Strings.Random(10, 2)
// random string, min 2 special chars, length 10
double x = "8".To<double>;
// converts string to any type
// (double in this case)
EnumType et = "Something".ToEnum<EnumType>;
// turns string into any enum
// (EnumType.Something in this case)
int? x = "5".ToNullable<int>;
// turns a string into any Nullable Type you want
// (int in this case)