Skip to content

Instantly share code, notes, and snippets.

View akimboyko's full-sized avatar
🙃

Akim Boyko akimboyko

🙃
View GitHub Profile
@akimboyko
akimboyko / DynamicGetEnumeratorUsingDLR.cs
Last active December 18, 2015 04:08
Dynamic definition of `GetEnumerator` using DLR without implementing `GetEnumerator` at compile time. Inspired by http://hotcode.org/speeches/c-deep-dive/
void Main()
{
dynamic something = new DynamicEnumerable();
something.DynamicMethod = new Action<string>(str => str.Dump());
"First `dynamic` GetEnumerator".Dump();
something.GetEnumerator = new Func<IEnumerator>(() =>
"test of dynamic enumerator".ToCharArray().GetEnumerator());
@akimboyko
akimboyko / NinjectFactoryWithParameters.cs
Created June 6, 2013 15:26
Ninject.Extensions.Factory with parameters tested using FluentAssertions
void Main()
{
using(var kernel = new StandardKernel(new GameModule()))
{
kernel.Load<FuncModule>();
kernel
.Get<GameArtifact>()
.Should().NotBeNull();
// from http://www.slideshare.net/SergeyTeplyakov/c-sharp-deep-dive
// by @STeplyakov
void Main()
{
var s3 = new S3();
s3.S2.X = 42;
s3.Dump();
}
struct S1
@akimboyko
akimboyko / howto.md
Last active December 17, 2015 23:29
#HotCode conference: Metaprogramming in .Net

Как метапрограмировать используя .Net на конференции #HotCode

@akimboyko
akimboyko / Fault.n
Created May 31, 2013 05:29
Nemerle macro sample: new syntax keyword `fault`
using Nemerle;
using Nemerle.Collections;
using Nemerle.Compiler;
using Nemerle.Text;
using Nemerle.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
@akimboyko
akimboyko / Howto.md
Last active February 2, 2019 00:02
Scripting: `eval()`-like for C# using Roslyn and ScriptCS
  • Install Chocolatey
  • Reopen console application (for example cmd.exe) to update %PATH% and other environment variables
  • Install ScriptCs using chocolatey
  • Save both Roslyn-EvalSample.csx and packages.config
  • Install dependencies using scriptcs -install
  • Run scriptcs .\Roslyn-EvalSample.csx
@akimboyko
akimboyko / Howto.md
Last active December 17, 2015 21:30
Search for `return null;` `yield return null;`, `return default(object);` and equivalents from ScriptCs/LinqPAD
  • Install Chocolatey
  • Reopen console application (for example cmd.exe) to update %PATH% and other environment variables
  • Install ScriptCs using chocolatey
  • Install dependencies using scriptcs -install -pre
  • Edit solutionPath const to point to solution
  • Run scriptcs .\SearchForReturnNulls.csx
@akimboyko
akimboyko / HmacSignatureRequiredAttribute.cs
Last active December 17, 2015 12:29
PostSharp custom architecture constraint ("HmacSignatureRequiredAttribute.cs" here) requires that every specified method of WebAPIcontroller ("SomeController.cs" here) contains both User Identity and HMAC (keyed-hash message authentication code ) signature. Otherwise compile-time error will happens
namespace CodeSmells.FakeWebApplication.Aspect
{
[Serializable]
[MulticastAttributeUsage(MulticastTargets.Class)]
public class HmacSignatureRequiredAttribute : ScalarConstraint
{
private readonly string[] _httpVerbs;
public HmacSignatureRequiredAttribute(string[] httpVerbs)
{
@akimboyko
akimboyko / AspectInfo.cs
Created May 16, 2013 03:00
Custom Architecture Constraints using PostSharp. Check that all properties of classes in namespace are: public, virtual, with both getter and setter. Based upon example from book "AOP in .Net" by @mgroves
// Multicast aspect to all properties of classes in namespace
[assembly: VirtualKeywordRequiredForInstanceProperties(
AttributeTargetTypes = "CodeSmells.FakeDataAccessLibrary.Entity.*",
AttributeTargetElements = MulticastTargets.Property)]
@akimboyko
akimboyko / Roslyn-SearchForReturnNull.cs
Last active December 17, 2015 06:29
Roslyn CTP: asynchronously search for `return null;` among all sources of solution.
// load workspace, i.e. solution from Visual Studio
using(var workspace = Workspace.LoadSolution(solutionPath))
{
// save a reference to original state
var origianlSolution = workspace.CurrentSolution;
// build syntax root and semantic model asynchronously in parallel
// for all documents from all projects
returnNulls =
origianlSolution