CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14

Thread: Dynamic casting

  1. #1
    Join Date
    Dec 2003
    Location
    Republic of Ireland
    Posts
    383

    Dynamic casting

    Hi,
    how can I dynamicly cast variables? Below is a sample that of course doesn't work but it gives you the picture what I'm trying to do.
    Code:
    DataTable table;
    
    //....
    
    foreach (DataColumn column in table.Columns)
    {
        switch (column.ColumnName)
        {
            case "MyProperty1":
                this.MyProperty1 = (this.MyProperty1.GetType())table.Rows[0][column.ColumnName];
                break;
    
            case "MyProperty2":
                this.MyProperty2 = (this.MyProperty2.GetType())table.Rows[0][column.ColumnName];
                break;
        }
    }
    Any help is appreciated,
    Thanx in advance.

  2. #2
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: Dynamic casting

    Code:
    MyType someReference = (MyType) someBaseReference;  // May throw exception
    
    // or
    
    MyType someReference = someBaseReference as MyType;  // May return null
    My hobby projects:
    www.rclsoftware.org.uk

  3. #3
    Join Date
    Dec 2003
    Location
    Republic of Ireland
    Posts
    383

    Re: Dynamic casting

    May be that you do not understand my problem. I sure know how to cast. The problem is that when I get System.Object I wanna cast it to various types. So I want to be able to check what that type is (or should be) and case dinamically at a runtime to proper type. To put it another way I want to decide during runtime to what type should I cast. I guess that I should use reflection? May be you can provide me some guidelines?

  4. #4
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: Dynamic casting

    Even if you could so that, you would have to assign the result of that cast to a reference who's type is known at compile time - so I'm not sure what you are trying to achieve.
    My hobby projects:
    www.rclsoftware.org.uk

  5. #5
    Join Date
    Dec 2003
    Location
    Republic of Ireland
    Posts
    383

    Re: Dynamic casting

    Look at my code sample! table.Rows[0][column.ColumnName] returns type object. But based on ColumnName I know to what class property I shall asign its value. I can get the type of that property using GetType but I do not know how to use it to perform cast.

    What I'm trying to acheive is that when type of that property will change I would not have to change the code that is used for setting them.

  6. #6
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: Dynamic casting

    Quote Originally Posted by Mr. Tomaszek
    What I'm trying to acheive is that when type of that property will change I would not have to change the code that is used for setting them.
    Ok, I think I understand now.

    Can you make use of the using keyword in this case?

    Code:
    using MyGeneralType = MySpecificType;
    
    class MySpecificType
    {
    }
    
    class MyOtherType
    {
    }
    
    //...
    
    class Xyz
    {
        public MyGeneralType TheProperty { /* ... */ }
    
        void f(object o)
        {
            this.TheProperty = (MyGeneralType)o;
        }
    }
    That way you can change what type MyGeneralType actually is by modifying just one line:

    Code:
    using MyGeneralType = MyOtherType;
    Last edited by Zaccheus; January 13th, 2007 at 02:50 PM.
    My hobby projects:
    www.rclsoftware.org.uk

  7. #7
    Join Date
    Dec 2003
    Location
    Republic of Ireland
    Posts
    383

    Re: Dynamic casting

    Thanx, this is close, but unfortunately it is not good enough for me. As suggested in my sample, each property has corresponding column in database. The code that is responsible for setting properties is actually a different module and must not be changed as type of properties will change. So the only way is to figure out property type (using GetType for instance) and use it somehow for casting - as in my sample from the first post. Thank you again for your effort and time. I just think that the way for achieving it is usage of reflection. Have you some experience with that?

  8. #8
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: Dynamic casting

    If you want the type of 'TheProperty' to change at runtime, have a look at the System.Reflection.Emit namespace:

    http://msdn2.microsoft.com/en-us/lib...tion.emit.aspx
    http://www.devhood.com/tutorials/tut...utorial_id=238
    Last edited by Zaccheus; January 13th, 2007 at 02:57 PM.
    My hobby projects:
    www.rclsoftware.org.uk

  9. #9
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: Dynamic casting

    When the assembly which contains the properties is compiled, those properties must have a specific type, that's why I don't understand when/why the types of the properties would change without your DLL being able to also change.
    My hobby projects:
    www.rclsoftware.org.uk

  10. #10
    Join Date
    Dec 2003
    Location
    Republic of Ireland
    Posts
    383

    Re: Dynamic casting

    Quote Originally Posted by Zaccheus
    When the assembly which contains the properties is compiled, those properties must have a specific type, that's why I don't understand when/why the types of the properties would change without your DLL being able to also change.
    In short, because we do not want to waste another hours to modify code that can be (hopefully) writen in the way that allow us to not modify it while on futre requirements change. It saves time, and it is safer in the meaning of possible introduction of (another) bugs. The structure obviously is much much more complex - so it is usefull to avoid additional modifications.

  11. #11
    Join Date
    Apr 2005
    Posts
    576

    Re: Dynamic casting

    Try something like:

    Code:
    GetType().GetProperty("Property1").SetValue(this, table.rows[0][Column.ColumnName], null);

  12. #12
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: Dynamic casting

    Wow, nice work klintan, that should do it !

    Quote Originally Posted by Mr. Tomaszek
    In short, because we do not want to waste another hours to modify code that can be (hopefully) writen in the way that allow us to not modify it while on futre requirements change. It saves time, and it is safer in the meaning of possible introduction of (another) bugs. The structure obviously is much much more complex - so it is usefull to avoid additional modifications.
    Yes, that is one of the disadvantages of C# not having header files. In C++ you could change one header file and that change would modify all the code in one go. That's a blessing and a curse, of course.
    My hobby projects:
    www.rclsoftware.org.uk

  13. #13
    Join Date
    Apr 2005
    Posts
    576

    Re: Dynamic casting

    Quote Originally Posted by klintan
    Try something like:

    Code:
    GetType().GetProperty("Property1").SetValue(this, table.rows[0][Column.ColumnName], null);
    One additional idea, if you are going down this route, writing generic database code, I suggest using reflection all the way.

    You could:
    a) reflect all properties of the class you are loading from the database, and assume that each property has the same name as the column
    b) like a, but instead of assuming that each property has the same name as the column, use attributes to define the column name, properties that not should be read from the database, and such things.

  14. #14
    Join Date
    Dec 2003
    Location
    Republic of Ireland
    Posts
    383

    Re: Dynamic casting

    Thank you all.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured