Skip to content

Instantly share code, notes, and snippets.

View walterlv's full-sized avatar
💤
Sleeping...

walterlv walterlv

💤
Sleeping...
View GitHub Profile
using System;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Interop;
// ReSharper disable IdentifierTypo
// ReSharper disable InconsistentNaming
@walterlv
walterlv / VisualScalingExtensions
Created April 25, 2019 09:21
Calculate WPF visual scaling ratios to user monitor device.
using System;
using System.Windows;
using System.Windows.Media;
namespace Walterlv
{
public static class VisualScalingExtensions
{
/// <summary>
/// 获取一个 <paramref name="visual"/> 在显示设备上的尺寸相对于自身尺寸的缩放比。
@walterlv
walterlv / ExceptionDescriptor.cs
Last active April 26, 2021 06:25
Get the key descriptor of an Exception instance so that we can determine whether two exceptions are the same.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
namespace Walterlv
{
/// <summary>
/// 包含一个 <see cref="Exception"/> 对象的关键特征,可使用此对象的实例判断两个不同的异常实例是否极有可能表示同一个异常。
/// </summary>
@walterlv
walterlv / SelfInstaller.cs
Created February 27, 2019 02:37
这是一个简单的自更新程序的类。使用此类型,程序初次运行的时候会安装自己,如果已安装旧版本会更新自己,如果已安装最新则直接运行。
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Security.Principal;
namespace Walterlv.Installing
{
@walterlv
walterlv / ContinuousPartOperation.cs
Last active October 12, 2022 08:26
An awaitable for waiting a part of a loop.
using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
namespace Walterlv
{
/// <summary>
/// 为一个持续操作中的一部分提供可异步等待的操作。
/// </summary>
@walterlv
walterlv / FileWatcher.cs
Last active March 19, 2019 01:53
FileWatcher that helps you to watch a single file change even if the file or it's owner folders does not exists.
using System;
using System.IO;
namespace Walterlv.IO
{
public sealed class FileWatcher
{
/// <summary>监视的文件。</summary>
private readonly FileInfo _file;
@walterlv
walterlv / AsyncBox.cs
Last active August 8, 2022 07:38
A UI container for async loading.
using System;
using System.Collections;
using System.ComponentModel;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Threading;
using Walterlv.Demo;
using System;
using System.Collections.Generic;
using Walterlv.Annotations;
namespace Walterlv
{
/// <summary>
/// 如果获取 <typeparamref name="TSource"/> 对应信息的过程比较耗时(例如反射),
/// 则可使用 <see cref="CachePool{TSource,TCache}"/> 对此过程进行缓存。
/// </summary>
@walterlv
walterlv / AwaiterInterfaces.cs
Created July 11, 2018 12:55
Custom awaiter with background UI thread
using System.Runtime.CompilerServices;
namespace Walterlv.Threading
{
/// <summary>
/// 表示一个可等待对象,如果一个方法返回此类型的实例,则此方法可以使用 `await` 异步等待。
/// </summary>
/// <typeparam name="TAwaiter">用于给 await 确定返回时机的 IAwaiter 的实例。</typeparam>
public interface IAwaitable<out TAwaiter> where TAwaiter : IAwaiter
{
@walterlv
walterlv / CommandArgumentAttribute.cs
Created October 21, 2017 01:40
Provide a reflection based wrapper for `Microsoft.Extensions.CommandlineUtils` Library.
using System;
namespace Mdmeta.Core
{
/// <summary>
/// Specify a property to receive argument of command from the user.
/// </summary>
[AttributeUsage(AttributeTargets.Property)]
public sealed class CommandArgumentAttribute : Attribute
{