-
C#-powered JavaScript Expression Evaluator
Here’s an interesting little class that I initially created to do dynamic arithmetic. However, after getting the concept up and running, it turns out to be a lot more powerful. The idea is to start up a hosted JavaScript runtime inside the CLR runtime and present it with string of JavaScript code for immediate inline…
-
Here’s a small extension to $ that programmatically selects the text beneath a jQuery object. It works cross-browser.
-
Extend Ruby Enumerables With An EachN Processor
Here’s a useful enhancement to arrays that allows you to process elements in a block n at a time. N is inferred by the arity of your block signature. Namaste…
-
Sleek CLI wrappers around Ruby libs
When using Ruby’s splat operator for method invocation, eg. my_method(*my_array), it takes the array and blows it out into the parameters required to fill the method’s signature. This makes it super easy to call methods from classes via the command line, by simply passing the arguments (ARGV) to a Ruby command line app and then…
-
Creating a “Supernatural” Self-Adapting REST Proxy in C#
.NET 4.0’s DynamicObject provides a quick and easy way to hook into and control how a call for a method is dispatched at runtime. This so called “late dispatch” capability is exactly what we need to easily create dynamic facades over out-of-process APIs, such as those of remote REST services. In this post, I’ll show…
-
Giving Your Data Some Higher Order Muscle With C#
Today a colleague and I were going through some code. I have recently been trying to impart to him the power and beauty in the functional programing paradigm in C#. Today, the opportunity to demonstrate it presented itself as I was showing him how to use extension methods to extend Entities and ValueObjects with a…
-
Ruby AOP in 12 lines of Code
First, we shim the Ruby Object class with a profiling aspect, in this case an additional method called profile that will wrap any existing method with timing code that we tell it to. Lets put this in a file called: aop_extension.rb. class Object def Object.profile symbol _symbol = (“rprof_” + symbol.to_s).to_sym alias_method…
-
Property getter/setter extraction from C# Types
The C# Expression API allows you to scrape property and method definitions from Types and work with them as external references. See here: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Linq.Expressions; using System.Dynamic; using System.Runtime.CompilerServices; public static class Extensions { public static Func GetPropertyFunction(this Type source, string name) { ParameterExpression param = Expression.Parameter(typeof(X), “arg”); MemberExpression member =…
-
My C# ObservableDictionary
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.ComponentModel;using System.Collections.Specialized; namespace DynamicSpikes{ public class ObservableDictionary : Dictionary, INotifyPropertyChanged, INotifyCollectionChanged { public event PropertyChangedEventHandler PropertyChanged = delegate { }; public event NotifyCollectionChangedEventHandler CollectionChanged = delegate { }; private void OnCollectionChanged(object sender, NotifyCollectionChangedAction action, object value) { if (CollectionChanged != null) { CollectionChanged(sender, new NotifyCollectionChangedEventArgs(action, value)); } } private void OnPropertyChanged(object sender, string propertyName) { if…
-
Runtime Stack Introspection with C#
public static string WhoCalledMe() { var st = new StackTrace(); var sf = st.GetFrame(1); var mb = sf.GetMethod(); return mb.Name; }