Skip to content

Instantly share code, notes, and snippets.

View lawrence-laz's full-sized avatar
👋
Hi there!

Laurynas Lazauskas lawrence-laz

👋
Hi there!
  • Lithuania, Vilnius
View GitHub Profile
@lawrence-laz
lawrence-laz / PowerShellOpener.cs
Created May 8, 2019 19:00
Tools that opens PowerShell in Unity project's Git root folder.
using System.Diagnostics;
using UnityEditor;
public static class PowerShellOpener
{
[MenuItem("Tools/PowerShell")]
private static void NewMenuOption()
{
var process = new ProcessStartInfo("powershell.exe")
{
@lawrence-laz
lawrence-laz / AsepriteUnityImporter.cs
Created April 17, 2020 19:12
AssetPostprocessor for Unity to save .aseprite files as .png sprites and import them.
using UnityEditor;
using UnityEngine;
public class AsepriteUnityImporter : AssetPostprocessor
{
static void OnPostprocessAllAssets(
string[] importedAssets,
string[] deletedAssets,
string[] movedAssets,
string[] movedFromAssetPaths)
@lawrence-laz
lawrence-laz / ActivatorUtilitiesConstructorTests.cs
Created July 24, 2020 08:32
Reproducing ActivatorUtilities.CreateInstance & ActivatorUtilitiesConstructorAttribute Bug
using Microsoft.Extensions.DependencyInjection;
using System;
using Xunit;
namespace UnitTests
{
public class ActivatorUtilitiesConstructorTests
{
public class Dependency1 { }
@lawrence-laz
lawrence-laz / GenericOverloadResolution.cs
Created July 30, 2020 05:00
Weakpoint of generic overload resolution in C#
class Foo
{
public static void Bar<TZoom>(TZoom zoom) where TZoom : IZoom { }
public static void Bar<TBoom, TLoom>(TBoom boom) where TBoom : IBoom<TLoom> { }
}
interface IZoom { }
interface IBoom<T> { }
class Zoom1 : IZoom { }
@lawrence-laz
lawrence-laz / InternalsVisibleTo.csproj
Last active January 26, 2021 19:54
Internals visible to other project
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
<_Parameter1>$(AssemblyName).Tests</_Parameter1>
</AssemblyAttribute>
</ItemGroup>
</Project>
@lawrence-laz
lawrence-laz / Startup.cs
Last active January 26, 2021 19:55
Add HttpMessageHandler to all HttpClients
services.AddSingleton<IConfigureOptions<HttpClientFactoryOptions>>(provider =>
{
return new ConfigureNamedOptions<HttpClientFactoryOptions>(name: null, options =>
{
options.HttpMessageHandlerBuilderActions.Add(builder =>
{
builder.AdditionalHandlers.Add(provider.GetRequiredService<YourHandler>());
});
});
});
@lawrence-laz
lawrence-laz / ConfigureOptions.cs
Created September 28, 2020 12:54
Configure Options
// Configure all named options without dependencies.
services.ConfigureAll<TOptions>(options =>
{
// Set your options here.
});
// Configure all named options with dependencies.
// Setting name to null is a key here.
services.AddSingleton<IConfigureOptions<TOptions>>(provider =>
{
@lawrence-laz
lawrence-laz / Startup.cs
Last active January 26, 2021 20:00
Set up CORS in ASP.NET Core on all controllers/methods.
// NuGet Microsoft.AspNet.Cors (5.2.7)
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(options =>
options.AddDefaultPolicy(builder =>
builder
.AllowAnyOrigin() // .WithOrigins(...) if .AllowCredentials
.AllowAnyMethod()
@lawrence-laz
lawrence-laz / AspNetCoreIntegrationTest.cs
Created March 25, 2021 10:02
Hosting a test application for automated integration tests.
[Theory]
[InlineData("Development")]
public async Task CallSwaggerEndpoint_ShouldReturnHttpOk(string environment)
{
// Arrange
using var sut = await Program.CreateHostBuilder(null)
.UseEnvironment(environment)
.UseDefaultServiceProvider((context, options) =>
{
@lawrence-laz
lawrence-laz / query-schema.sql
Last active August 17, 2021 06:13
Query MS SQL Server database schema
select schema_name(tab.schema_id) as schema_name,
tab.name as table_name,
col.column_id,
col.name as column_name,
t.name as data_type,
col.max_length,
col.precision
from sys.tables as tab
inner join sys.columns as col
on tab.object_id = col.object_id