locked
Make my own dynamic? RRS feed

  • Question

  • I want to have a class that looks like a dynamic datatype.  How do i go about doing that?

    Example:

    bool bTest = false;

    int iTest = 3;

    MyDynamic myDynamic;

    myDynamic = bTest;

    myDynamic = iTest;

    Saturday, January 15, 2011 1:45 AM

Answers

  • You can create conversions from different types to/from your type:

    class MyDynamic
    {
      bool? boolValue;
      int? intValue;
    
      public static implicit operator MyDynamic(bool value)
      {
        return new MyDynamic { boolValue = value };
      }
    
      public static implicit operator MyDynamic(int value)
      {
        return new MyDynamic { intValue = value };
      }
    
      public static explicit operator bool(MyDynamic value)
      {
        return (bool)value.boolValue;
      }
    
      public static explicit operator int(MyDynamic value)
      {
        return (int)value.intValue;
      }
    }
    
    • Proposed as answer by Mike Dos Zhang Friday, January 21, 2011 7:15 AM
    • Marked as answer by Mike Dos Zhang Tuesday, February 1, 2011 3:19 AM
    Monday, January 17, 2011 9:02 AM
  • If you are using .NET 4, dynamic will do this perfectly.

     

    In .NET <=3.5, you can handle the assignments by using object, but you won't be able to use the data with methods, etc, without converting back to their proper type.

     

    There is no directly way to have a usable dynamic type in .NET 3.5 and earlier - it was one of the major features added with C#/.NET 4.0.


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Proposed as answer by Mike Dos Zhang Tuesday, January 25, 2011 2:40 PM
    • Marked as answer by Mike Dos Zhang Tuesday, February 1, 2011 3:19 AM
    Saturday, January 15, 2011 2:25 AM
  • Check the following article on MSDN: http://msdn.microsoft.com/en-us/library/ee461504.aspx
    • Proposed as answer by Mike Dos Zhang Tuesday, January 25, 2011 2:40 PM
    • Marked as answer by Mike Dos Zhang Tuesday, February 1, 2011 3:19 AM
    Sunday, January 16, 2011 6:01 PM
  • Shaggygi

    Dynamic is a keyword which uses inbuild reflection, so if you want to do something the same, use reflection.

    Although it will mark you as a not so very well developer doing that.

    Both are in Framework 4 for parity with VB and it uses both by VB Net and C# developers not wanted behavior from VB6 (Option Strict Off)


    Success
    Cor
    • Proposed as answer by Mike Dos Zhang Friday, January 21, 2011 7:16 AM
    • Marked as answer by Mike Dos Zhang Tuesday, February 1, 2011 3:19 AM
    Monday, January 17, 2011 7:59 AM
  • In 3.5, var keyword is also the same. We can assign any type of value to the var. the code will  looks like,

     

    var iNum = 1;

    var bValue = true;

    var sName ="Reed";

     


    With Regards, DP

    DP,

    However, the var keyword is very different.

     

    This produces exactly the same code as:

     

    int iNum = 1;

    bool bValue = true;

    string sName = "Reed";

     

    "var" is converted at compile time to the known type.

     


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Proposed as answer by Mike Dos Zhang Tuesday, January 25, 2011 2:40 PM
    • Marked as answer by Mike Dos Zhang Tuesday, February 1, 2011 3:19 AM
    Sunday, January 16, 2011 7:01 PM

All replies


  • I don't think so, but maybe you should elaborate on why the following is not sufficient:
     
       bool bTest = false;
       int iTest = 3;
     
       dynamic myDynamic;
       myDynamic = bTest;
       myDynamic = iTest;

    --
    Mike
    Saturday, January 15, 2011 2:03 AM
  • If you are using .NET 4, dynamic will do this perfectly.

     

    In .NET <=3.5, you can handle the assignments by using object, but you won't be able to use the data with methods, etc, without converting back to their proper type.

     

    There is no directly way to have a usable dynamic type in .NET 3.5 and earlier - it was one of the major features added with C#/.NET 4.0.


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Proposed as answer by Mike Dos Zhang Tuesday, January 25, 2011 2:40 PM
    • Marked as answer by Mike Dos Zhang Tuesday, February 1, 2011 3:19 AM
    Saturday, January 15, 2011 2:25 AM
  • Check the following article on MSDN: http://msdn.microsoft.com/en-us/library/ee461504.aspx
    • Proposed as answer by Mike Dos Zhang Tuesday, January 25, 2011 2:40 PM
    • Marked as answer by Mike Dos Zhang Tuesday, February 1, 2011 3:19 AM
    Sunday, January 16, 2011 6:01 PM
  • If you are using .NET 4, dynamic will do this perfectly.

     

    In .NET <=3.5, you can handle the assignments by using object, but you won't be able to use the data with methods, etc, without converting back to their proper type.

     

    There is no directly way to have a usable dynamic type in .NET 3.5 and earlier - it was one of the major features added with C#/.NET 4.0.


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Hi Reed,

    I want to add some more point to your post,

    In 3.5, var keyword is also the same. We can assign any type of value to the var. the code will  looks like,

     

    var iNum = 1;

    var bValue = true;

    var sName ="Reed";

     


    With Regards, DP
    Sunday, January 16, 2011 6:05 PM
  • In 3.5, var keyword is also the same. We can assign any type of value to the var. the code will  looks like,

     

    var iNum = 1;

    var bValue = true;

    var sName ="Reed";

     


    With Regards, DP

    DP,

    However, the var keyword is very different.

     

    This produces exactly the same code as:

     

    int iNum = 1;

    bool bValue = true;

    string sName = "Reed";

     

    "var" is converted at compile time to the known type.

     


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Proposed as answer by Mike Dos Zhang Tuesday, January 25, 2011 2:40 PM
    • Marked as answer by Mike Dos Zhang Tuesday, February 1, 2011 3:19 AM
    Sunday, January 16, 2011 7:01 PM
  • Thanks for the detailed information Reed,

    "var" is converted at compile time to the known type.

    Definitely this point will helps to more no of MSDN readers.

     

     


    With Regards, DP
    Monday, January 17, 2011 5:23 AM
  • Shaggygi

    Dynamic is a keyword which uses inbuild reflection, so if you want to do something the same, use reflection.

    Although it will mark you as a not so very well developer doing that.

    Both are in Framework 4 for parity with VB and it uses both by VB Net and C# developers not wanted behavior from VB6 (Option Strict Off)


    Success
    Cor
    • Proposed as answer by Mike Dos Zhang Friday, January 21, 2011 7:16 AM
    • Marked as answer by Mike Dos Zhang Tuesday, February 1, 2011 3:19 AM
    Monday, January 17, 2011 7:59 AM
  • You can create conversions from different types to/from your type:

    class MyDynamic
    {
      bool? boolValue;
      int? intValue;
    
      public static implicit operator MyDynamic(bool value)
      {
        return new MyDynamic { boolValue = value };
      }
    
      public static implicit operator MyDynamic(int value)
      {
        return new MyDynamic { intValue = value };
      }
    
      public static explicit operator bool(MyDynamic value)
      {
        return (bool)value.boolValue;
      }
    
      public static explicit operator int(MyDynamic value)
      {
        return (int)value.intValue;
      }
    }
    
    • Proposed as answer by Mike Dos Zhang Friday, January 21, 2011 7:15 AM
    • Marked as answer by Mike Dos Zhang Tuesday, February 1, 2011 3:19 AM
    Monday, January 17, 2011 9:02 AM
  • Hi  shaggygi,

     

    Have you clear about this doubt?

     

    If there's any concern, please feel free to let me know.

     

    Have a nice day!


    Mike [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, January 20, 2011 8:42 AM