A lightweight C# utility library.
The library is available on nuget.org via package name PureLib
, PureLib.Legacy
and PureLib.EntityFramework
.
To install PureLib, run the following command in the Package Manager Console
PM> Install-Package PureLib
PM> Install-Package PureLib.Legacy
PM> Install-Package PureLib.EntityFramework
More information about NuGet package avaliable at https://nuget.org/packages/PureLib
- WPF
- Web
- Utility
NotifyObject
implements INotifyPropertyChanged
, which enables you to raise changes of properties.
public string StatusBarText {
get { return _statusBarText; }
set {
_statusBarText = value;
RaiseChange(() => StatusBarText); // or RaiseChange("StatusBarText");
}
}
ViewModelBase
inherits NotifyObject
, which is designed to be the base class of ViewModels in MVVM pattern.
public class MainWindowViewModel : ViewModelBase {
public ObservableCollection<string> Files { get; set; }
private void OnTaskStarted(object sender, TaskStartedEventArgs e) {
RunOnUIThread(() => {
if (!Files.Contains(e.File))
Files.Add(e.File);
});
}
}
RelayCommand
implements ICommand
, which could be bound to UI controls.
private ICommand _openDescriptionCommand;
public ICommand OpenDescriptionCommand {
get {
if (_openDescriptionCommand == null)
_openDescriptionCommand = new RelayCommand(p => {
OpeningDescription(this, new EventArgs<string>(((WatFile)p).Description));
}, p => !((WatFile)p).Description.IsNullOrEmpty());
return _openDescriptionCommand;
}
}
SingleInstanceApp
inherits Application
. The application inherits SingleInstanceApp
will not be able to run multiple instances.
public partial class App : SingleInstanceApp {
}
PureLib provides commonly used converters for UI bindings.
BooleanToVisibilityConverter
InverseBooleanConverter
WebRequester
is a wrapper for easily using HttpWebRequest
. It features auto retry with specified times.
_requester = new WebRequester(_cookiePersister.CookieContainer) {
UserAgent = agent,
Referer = referer,
Encoding = Encoding.GetEncoding("gbk"),
RetryInterval = 2000,
RetryLimit = 5
};
_requester.SetRequest += (s, e) => {
e.Data.Proxy = null;
};
_requester.GotResponse += (s, e) => {
if (!_ignoreCookie)
_cookiePersister.Update(e.Data);
};
WebDownloader
contains essential functions of a download manager. It can dispatch any number of threads to download concurrently.
WebDownloader downloader = new WebDownloader(Global.Config.ThreadCount, null, false);
downloader.DownloadCompleting += OnDownloadCompleting;
downloader.AddItems(_itemPostMaps.Keys.ToList());
Get running time of specfic code segment:
TimeSpan ts = Utility.GetExecutingDuration(() => {
for (int i = 0; i < 1000000; i++) {
}
});
Convert a wildcard to a regular expression:
string regex = "*.txt".WildcardToRegex();
Convert a byte array to a string in hex format:
string hex = Encoding.UTF8.GetBytes("hello").ToHexString();
Convert a string of enum list to an enum array:
DayOfWeek[] days = "Sunday,Saturday".ToEnum<DayOfWeek>();
Get human-friendly text from an exception:
try {
} catch (Exception ex) {
string text = ex.GetTraceText();
}
Shortcuts for reading/writing files on disk:
"D:\\data.bin".WriteBinary(new byte[] { 0x00, 0x01 });
byte[] data = "D:\\data.bin".ReadBinary();
"D:\\text.txt".WriteText("hello", Encoding.Default);
string text = "D:\\text.txt".ReadText();
MIT