Skip to content

Instantly share code, notes, and snippets.

@demircimurat
Last active August 29, 2015 14:12
Show Gist options
  • Select an option

  • Save demircimurat/0f4cbc23b7a168f16c84 to your computer and use it in GitHub Desktop.

Select an option

Save demircimurat/0f4cbc23b7a168f16c84 to your computer and use it in GitHub Desktop.

Revisions

  1. Murat Demircioglu revised this gist Jan 4, 2015. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions gistfile1.cs
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,8 @@
    /*
    * based on Jorge Fioranelli's code..
    * http://blog.jorgef.net/2011/06/converting-any-object-to-dynamic.html
    */

    public static class Extensions
    {
    public static dynamic ToDynamic(this object value)
  2. Murat Demircioglu revised this gist Jan 4, 2015. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions gistfile1.cs
    Original file line number Diff line number Diff line change
    @@ -28,8 +28,7 @@ public static bool IsListOrArray(this object value)
    return true;
    }

    var valueType = value.GetType ();
    if (valueType.IsArray) {
    if (value.GetType().IsArray) {
    return true;
    }

  3. Murat Demircioglu created this gist Jan 4, 2015.
    38 changes: 38 additions & 0 deletions gistfile1.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    public static class Extensions
    {
    public static dynamic ToDynamic(this object value)
    {
    if (value.IsListOrArray ()) {

    var list = new List<ExpandoObject> ();
    IEnumerable enumerable = value as IEnumerable;
    foreach (object o in enumerable) {
    list.Add (o.ToDynamic ());
    }

    return (dynamic) list;
    }

    IDictionary<string, object> expando = new ExpandoObject ();

    foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(value.GetType())) {
    expando.Add (property.Name, property.GetValue (value));
    }

    return (dynamic) expando;
    }

    public static bool IsListOrArray(this object value)
    {
    if(value is IList && value.GetType().IsGenericType) {
    return true;
    }

    var valueType = value.GetType ();
    if (valueType.IsArray) {
    return true;
    }

    return false;
    }
    }