Skip to content
forked from eriforce/PureLib

A lightweight C# utility library

License

Notifications You must be signed in to change notification settings

CodeFork/PureLib

 
 

Repository files navigation

PureLib

Build status NuGet Version License MIT

A lightweight C# utility library.

Download

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

Features

Notify Object

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");
    }
}

View Model Base

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);
            });
        }
    }

Relay Command

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;
    }
}

Single Instance App

SingleInstanceApp inherits Application. The application inherits SingleInstanceApp will not be able to run multiple instances.

public partial class App : SingleInstanceApp { 
}

Converters

PureLib provides commonly used converters for UI bindings.

  • BooleanToVisibilityConverter
  • InverseBooleanConverter

Web Requester

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);
};

Web Downloader

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());

Utility

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();

License

MIT

About

A lightweight C# utility library

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 100.0%