Skip to content

Instantly share code, notes, and snippets.

using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Interactivity;
[TypeConstraint(typeof(FrameworkElement))]
public class UpdateTargetAction : TriggerAction<FrameworkElement>
{
public static readonly DependencyProperty PropertyNameProperty =
DependencyProperty.Register(nameof(PropertyName), typeof(string), typeof(UpdateTargetAction));
using System;
using System.Reflection;
using System.Reflection.Emit;
public static class DynamicActivatorFactory
{
private static readonly AssemblyBuilder AssemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(
new AssemblyName("DynamicActivator"),
AssemblyBuilderAccess.Run);
@usausa
usausa / MainWindow.xaml
Created February 16, 2018 07:35
Attached property binding
<Window x:Class="WpfApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<StackPanel>
<Label Content="{Binding Path=Content.(local:ViewProperty.Title), Mode=OneWay, Source={x:Reference Container}}"/>
@usausa
usausa / Fill.cs
Last active April 14, 2018 07:42
public static unsafe byte[] Fill(this byte[] array, int offset, int length, byte value)
{
if ((length <= 0) || (array == null))
{
return array;
}
fixed (byte* pSrc = &array[offset])
{
*pSrc = value;
public class LowercaseControllerModelConvention : IControllerModelConvention
{
public void Apply(ControllerModel controller)
{
controller.ControllerName = controller.ControllerName.ToLower();
foreach (var action in controller.Actions)
{
action.ActionName = action.ActionName.ToLower();
}
}
// Good
public static async Task<T> UseTemporary<T>(Func<string, Task<T>> func)
{
var path = Path.GetTempFileName();
try
{
return await func(path);
}
finally
{
@usausa
usausa / ConvertEnumerableBenchmark.cs
Created November 2, 2018 14:04
int[100] to List<int> with convert(NOP)
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using BenchmarkDotNet.Attributes;
[Config(typeof(BenchmarkConfig))]
public class ConvertEnumerableBenchmark
{
@usausa
usausa / PolicyExample.cs
Last active February 20, 2019 06:20
Static field in generic type with policy
using System;
public interface IPolicy
{
string ToName(string source);
}
public sealed class DefaultPolicy : IPolicy
{
public string ToName(string source) => source;
@usausa
usausa / AppViewModelBase.cs
Created February 26, 2019 08:01
dseign-time BindingContext and runtime resolve
public class AppViewModelBase<T> : ViewModelBase
where T : AppViewModelBase<T>
{
public static T DesignInstance { get; } = null; // For design
}
@usausa
usausa / .editorconfig
Created March 10, 2019 04:24
EditorConfig for cs
###############################
# Core EditorConfig Options #
###############################
root = true
# All files
[*]
indent_style = space