Skip to content

Instantly share code, notes, and snippets.

@JohannesEH
JohannesEH / Example.cs
Created October 27, 2011 22:21
How to make a non-nullable type wrapper in C#, today… (doesn't work, for blog)
public void DoStuff(object input)
{
if (input == null)
throw new ArgumentNullException("input");
// Method implementation...
}
@JohannesEH
JohannesEH / Implementation.cs
Created October 27, 2011 22:05
Extension Types for easier (and strongly typed) Attribute checks
public static class AttributeExtensions
{
// Attribute.IsDefined shortcuts
public static bool HasAttribute<T>(this object element) where T : Attribute
{ return Attribute.IsDefined(element.GetType(), typeof(T)); }
public static bool HasAttribute<T>(this object element, bool inherit) where T : Attribute
{ return Attribute.IsDefined(element.GetType(), typeof(T), inherit); }
public static bool HasAttribute<T>(this Assembly element) where T : Attribute
{ return Attribute.IsDefined(element, typeof(T)); }
public static bool HasAttribute<T>(this Assembly element, bool inherit) where T : Attribute
@JohannesEH
JohannesEH / ServiceConfiguration.xml
Created October 27, 2011 21:43
Host name problems in WCF service metadata
<system.serviceModel>
<services>
<service name="Example.ProblemService" behaviorConfiguration="CustomBehavior">
<endpoint contract="Example.IProblemService" binding="wsHttpBinding" bindingConfiguration="ssl" />
<endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex"/>
</service>
</services>
<bindings>
<wshttpbinding>
@JohannesEH
JohannesEH / Implementation.cs
Created October 25, 2011 09:21
JSON to dynamic object function
private static dynamic CreateDynamicFromObjectGraph(object input)
{
if(input is IDictionary<string, object>)
{
var result = (IDictionary<string, object>)(new ExpandoObject());
foreach (var pair in (IDictionary<string, object>)input)
result.Add(pair.Key, CreateDynamicFromObjectGraph(pair.Value));
return result;