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 / BaseBehaviour.cs
Created November 30, 2018 18:00
Automatically get components in Unity using [GetComponent] attribute
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEngine;
public class BaseBehaviour : MonoBehaviour
{
protected virtual void OnEnable()
{
@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 / 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 / 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 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 / 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 / CopyFolder.csproj
Last active January 20, 2023 15:58
Examples for .csproj
<Project Sdk="Microsoft.NET.Sdk.Web">
<ItemGroup>
<!-- Copies all content from wwwroot folder to output folder specified in LinkBase -->
<None Include="wwwroot\**" Exclude="wwwroot\**\*.cs" CopyToOutputDirectory="Always" LinkBase="wwwroot\" />
<!-- If above dosn't work, maybe non-SDK style project, or solution has some non-SDK style projects, try this -->
<None Include="wwwroot\**" CopyToOutputDirectory="Always" Link="wwwroot/%(RecursiveDir)%(Filename)%(Extension)" />
</ItemGroup>
</Project>