Skip to content

Instantly share code, notes, and snippets.

@vendettamit
vendettamit / Create T object and invoking method.cs
Created September 24, 2013 15:45
Creating a generic object via reflection and invoking a method on it.
/// <summary>
/// The deserialize to generic list.
/// </summary>
/// <param name="jsonString">
/// The json string.
/// </param>
/// <typeparam name="TReturnType">Type of a list of type TItemType
/// </typeparam>
/// <typeparam name="TItemType">Type of generic argument to list
/// </typeparam>
@vendettamit
vendettamit / SqlColumnAndValueTypeFormatter.cs
Created October 1, 2013 14:31
Sql columns and value parser
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleForAnything
{
public class Program
{
@vendettamit
vendettamit / StringToBooleanJsonConverter
Created October 8, 2013 15:36
Custom converter for String "0" to boolean conversion during parsing of JSON using JSON.Net
public class StringBooleanConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return objectType == typeof(string) || objectType == typeof(bool);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
byte proposedValue;
@vendettamit
vendettamit / InMemoryNunitTestRunner
Created July 25, 2014 09:51
InMemory NunitTestRunner
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
@vendettamit
vendettamit / CustomAppDomain.cs
Created September 20, 2014 14:51
Creating custom AppDomain
public class DomainController
{
public void CreateDomain()
{
string pathToDlls = Path.GetDirectoryName(new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath);
Trace.WriteLine(AppDomain.CurrentDomain.FriendlyName);
AppDomainSetup domainSetup = new AppDomainSetup { };
domainSetup.ApplicationBase = pathToDlls;
this.testDomain = AppDomain.CreateDomain(Guid.NewGuid().ToString(), null, domainSetup, this.GetPermissionSet());
@vendettamit
vendettamit / AssemblyLoader.cs
Created September 20, 2014 15:01
Load Assembly in AppDomain
this.testDomain.Load(Assembly.GetExecutingAssembly().GetName());
var instance = (CompilerService)this.testDomain.CreateInstanceAndUnwrap(
typeof(CompilerService).Assembly.FullName,
typeof(CompilerService).FullName);
return instance.CompileAndRunTestsInOtherDomain(args);
public class LogInjectionModule : Module {
protected override void AttachToComponentRegistration(IComponentRegistry registry,
IComponentRegistration registration) {
registration.Preparing += OnComponentPreparing;
var implementationType = registration.Activator.LimitType;
var injectors = BuildInjectors(implementationType).ToArray();
if (!injectors.Any()) {
return;
public class DynamicParserJson
{
public static object ConvertJTokenToObject(JToken token)
{
if (token is JValue)
{
return ((JValue)token).Value;
}
if (token is JObject)
{
@vendettamit
vendettamit / MethodInterceptionViaAttributesDynamicProxy.cs
Created September 2, 2015 20:26
Method interception via attributes using dynamic proxy. Argument null checking at run time via method attributes. DynamicProxy2, Castle Core.
public class Program
{
static void Main(string[] args)
{
Bootstrapper.Configure();
var container = Bootstrapper.Container;
var obj = container.Resolve<Program>();
var newString = obj.UseMe(null);
@vendettamit
vendettamit / ThreadSwitchingWithAutoResetEvent.cs
Created September 3, 2015 18:23
Sample is to show the Thread switching using AutoResetEvent.
public class DeviceMonitorSignaling
{
readonly object _lockObj = new object();
EventWaitHandle searchingHandle;
EventWaitHandle monitoringHandle;
bool _running;
bool _monitoring;
volatile Device device;