Dynamic Casting

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Philipp Schumann

    Dynamic Casting

    Hi,

    I have a need for "dynamic type casting": in other words, in a "MyConvert"
    method I get passed an Object "value" and a Type "type" and the method
    should attempt to convert value into type.

    Of course it first tries to obtain the appropriate TypeConverter. However,
    for some types there are no applicable type converters.
    Consider a custom class "Dummy" from a linked assembly (in my scenario,
    neither the Dummy type nor its assembly are "known", meaning any Type from
    any assembly could be passed). There is no TypeConverter associated with
    Dummy, however, it defines implicit cast operators to convert between Dummy
    and String. I would like to know whether there is a standard way of
    performing such a "dynamic" or "runtime" cast. I tried Convert.ChangeT ype()
    but that method only works with IConvertible types---and completely ignores
    any cast operators defined on any of the two types (that of value, and the
    target Type).

    Any ideas?
    Many thanks,
    Phil


  • Philipp Schumann

    #2
    Re: Dynamic Casting

    I gather there is no built-in way to do this?...

    "Philipp Schumann" <phil@mokka.org > schrieb im Newsbeitrag
    news:uP68KrCRFH A.3716@TK2MSFTN GP14.phx.gbl...[color=blue]
    > Hi,
    >
    > I have a need for "dynamic type casting": in other words, in a "MyConvert"
    > method I get passed an Object "value" and a Type "type" and the method
    > should attempt to convert value into type.
    >
    > Of course it first tries to obtain the appropriate TypeConverter. However,
    > for some types there are no applicable type converters.
    > Consider a custom class "Dummy" from a linked assembly (in my scenario,
    > neither the Dummy type nor its assembly are "known", meaning any Type from
    > any assembly could be passed). There is no TypeConverter associated with
    > Dummy, however, it defines implicit cast operators to convert between
    > Dummy and String. I would like to know whether there is a standard way of
    > performing such a "dynamic" or "runtime" cast. I tried
    > Convert.ChangeT ype() but that method only works with IConvertible
    > types---and completely ignores any cast operators defined on any of the
    > two types (that of value, and the target Type).
    >
    > Any ideas?
    > Many thanks,
    > Phil
    >[/color]


    Comment

    • Dan

      #3
      Re: Dynamic Casting

      How would the code following the cast know how to treat a reference
      that it does not have type information for at compile time? I'm pretty
      sure that there is no way to dynamically cast the way you want it to.
      I have to do similar things to what you describe in my projects. I
      have a manager that will call objects that were created well after the
      manager was compiled. I have found that I can work around this issue
      using interfaces/base classes and solve the problems in a more type
      safe way. To convert your dummy class to a string the easiest way to
      do what you want is override .ToString(). In that case you can use the
      base class to provide the interface to provide the conversion.

      If all of your dummy classes implement the same conversions, try
      creating and implementing 1 or more interfaces like IToDecimal,
      IToDateTime etc. It sucks that you cannot define operators in an
      interface, but you can do the same work with methods and properties
      (though not as pretty)

      HTH,
      Dan

      Comment

      • James Curran

        #4
        Re: Dynamic Casting

        You don't need a dynamic cast. You need to fix your design....
        [color=blue][color=green]
        >> [class Dummy] defines implicit cast operators to convert between Dummy[/color][/color]
        and String.

        implicit cast operators? eek.. Replacement them with explicit
        conversion functions, say ToString & FromString. Define an interface, have
        Dummy (and your other classes) implement that interface.

        interface IToFromString
        {
        string ToString();
        void FromString(stri ng str);
        }

        public class Dummy : IToFromString
        {
        public override string ToString() {.... }
        public void FromString(stri ng str) {.....}
        }


        ThatFunctionInA DifferentAssemb ly(IToFromStrin g obj)
        {
        Console.WriteLi ne(obj.ToString ());
        }

        --
        --
        Truth,
        James Curran
        [erstwhile VC++ MVP]
        Home: www.noveltheory.com Work: www.njtheater.com
        Blog: www.honestillusion.com Day Job: www.partsearch.com

        "Philipp Schumann" <phil@mokka.org > wrote in message
        news:uP68KrCRFH A.3716@TK2MSFTN GP14.phx.gbl...[color=blue]
        > Hi,
        >
        > I have a need for "dynamic type casting": in other words, in a "MyConvert"
        > method I get passed an Object "value" and a Type "type" and the method
        > should attempt to convert value into type.
        >
        > Of course it first tries to obtain the appropriate TypeConverter. However,
        > for some types there are no applicable type converters.
        > Consider a custom class "Dummy" from a linked assembly (in my scenario,
        > neither the Dummy type nor its assembly are "known", meaning any Type from
        > any assembly could be passed). There is no TypeConverter associated with
        > Dummy, however, it defines implicit cast operators to convert between[/color]
        Dummy[color=blue]
        > and String. I would like to know whether there is a standard way of
        > performing such a "dynamic" or "runtime" cast. I tried[/color]
        Convert.ChangeT ype()[color=blue]
        > but that method only works with IConvertible types---and completely[/color]
        ignores[color=blue]
        > any cast operators defined on any of the two types (that of value, and the
        > target Type).
        >
        > Any ideas?
        > Many thanks,
        > Phil
        >
        >[/color]


        Comment

        • Philipp Schumann

          #5
          Re: Dynamic Casting

          Who says it is my design? My design just wants to work with ANY scenario, so
          whether or not I _personally_ prefer the convenience of cast operators
          (which can mean a lot _less_ overload definitions for many functions)
          doesn't matter here. That said, the Dummy class was a moniker for all the
          unknown types the API will need to work with, rather than a smallish design
          of my own which I can change as I (or you) wish...

          The whole issue actually makes me wonder whether type conversions are C#
          syntactic sugar rather than supported at the framework level. Almost
          certainly seems to be the case, will have to check that.

          "James Curran" <jamescurran@mv ps.org> schrieb im Newsbeitrag
          news:%23o3yJhPR FHA.3496@TK2MSF TNGP12.phx.gbl. ..[color=blue]
          > You don't need a dynamic cast. You need to fix your design....
          >[color=green][color=darkred]
          >>> [class Dummy] defines implicit cast operators to convert between Dummy[/color][/color]
          > and String.
          >
          > implicit cast operators? eek.. Replacement them with explicit
          > conversion functions, say ToString & FromString. Define an interface,
          > have
          > Dummy (and your other classes) implement that interface.
          >
          > interface IToFromString
          > {
          > string ToString();
          > void FromString(stri ng str);
          > }
          >
          > public class Dummy : IToFromString
          > {
          > public override string ToString() {.... }
          > public void FromString(stri ng str) {.....}
          > }
          >
          >
          > ThatFunctionInA DifferentAssemb ly(IToFromStrin g obj)
          > {
          > Console.WriteLi ne(obj.ToString ());
          > }
          >
          > --
          > --
          > Truth,
          > James Curran
          > [erstwhile VC++ MVP]
          > Home: www.noveltheory.com Work: www.njtheater.com
          > Blog: www.honestillusion.com Day Job: www.partsearch.com
          >
          > "Philipp Schumann" <phil@mokka.org > wrote in message
          > news:uP68KrCRFH A.3716@TK2MSFTN GP14.phx.gbl...[color=green]
          >> Hi,
          >>
          >> I have a need for "dynamic type casting": in other words, in a
          >> "MyConvert"
          >> method I get passed an Object "value" and a Type "type" and the method
          >> should attempt to convert value into type.
          >>
          >> Of course it first tries to obtain the appropriate TypeConverter.
          >> However,
          >> for some types there are no applicable type converters.
          >> Consider a custom class "Dummy" from a linked assembly (in my scenario,
          >> neither the Dummy type nor its assembly are "known", meaning any Type
          >> from
          >> any assembly could be passed). There is no TypeConverter associated with
          >> Dummy, however, it defines implicit cast operators to convert between[/color]
          > Dummy[color=green]
          >> and String. I would like to know whether there is a standard way of
          >> performing such a "dynamic" or "runtime" cast. I tried[/color]
          > Convert.ChangeT ype()[color=green]
          >> but that method only works with IConvertible types---and completely[/color]
          > ignores[color=green]
          >> any cast operators defined on any of the two types (that of value, and
          >> the
          >> target Type).
          >>
          >> Any ideas?
          >> Many thanks,
          >> Phil
          >>
          >>[/color]
          >
          >[/color]


          Comment

          • Philipp Schumann

            #6
            Re: Dynamic Casting

            Who says it is my design? My design just wants to work with ANY scenario, so
            whether or not I _personally_ prefer the convenience of cast operators
            (which can mean a lot _less_ overload definitions for many functions)
            doesn't matter here. That said, the Dummy class was a moniker for all the
            unknown types the API will need to work with, rather than a smallish design
            of my own which I can change as I (or you) wish...

            The whole issue actually makes me wonder whether type conversions are C#
            syntactic sugar rather than supported at the framework level. Almost
            certainly seems to be the case, will have to check that.

            "James Curran" <jamescurran@mv ps.org> schrieb im Newsbeitrag
            news:%23o3yJhPR FHA.3496@TK2MSF TNGP12.phx.gbl. ..[color=blue]
            > You don't need a dynamic cast. You need to fix your design....
            >[color=green][color=darkred]
            >>> [class Dummy] defines implicit cast operators to convert between Dummy[/color][/color]
            > and String.
            >
            > implicit cast operators? eek.. Replacement them with explicit
            > conversion functions, say ToString & FromString. Define an interface,
            > have
            > Dummy (and your other classes) implement that interface.
            >
            > interface IToFromString
            > {
            > string ToString();
            > void FromString(stri ng str);
            > }
            >
            > public class Dummy : IToFromString
            > {
            > public override string ToString() {.... }
            > public void FromString(stri ng str) {.....}
            > }
            >
            >
            > ThatFunctionInA DifferentAssemb ly(IToFromStrin g obj)
            > {
            > Console.WriteLi ne(obj.ToString ());
            > }
            >
            > --
            > --
            > Truth,
            > James Curran
            > [erstwhile VC++ MVP]
            > Home: www.noveltheory.com Work: www.njtheater.com
            > Blog: www.honestillusion.com Day Job: www.partsearch.com
            >
            > "Philipp Schumann" <phil@mokka.org > wrote in message
            > news:uP68KrCRFH A.3716@TK2MSFTN GP14.phx.gbl...[color=green]
            >> Hi,
            >>
            >> I have a need for "dynamic type casting": in other words, in a
            >> "MyConvert"
            >> method I get passed an Object "value" and a Type "type" and the method
            >> should attempt to convert value into type.
            >>
            >> Of course it first tries to obtain the appropriate TypeConverter.
            >> However,
            >> for some types there are no applicable type converters.
            >> Consider a custom class "Dummy" from a linked assembly (in my scenario,
            >> neither the Dummy type nor its assembly are "known", meaning any Type
            >> from
            >> any assembly could be passed). There is no TypeConverter associated with
            >> Dummy, however, it defines implicit cast operators to convert between[/color]
            > Dummy[color=green]
            >> and String. I would like to know whether there is a standard way of
            >> performing such a "dynamic" or "runtime" cast. I tried[/color]
            > Convert.ChangeT ype()[color=green]
            >> but that method only works with IConvertible types---and completely[/color]
            > ignores[color=green]
            >> any cast operators defined on any of the two types (that of value, and
            >> the
            >> target Type).
            >>
            >> Any ideas?
            >> Many thanks,
            >> Phil
            >>
            >>[/color]
            >
            >[/color]


            Comment

            • Jon Skeet [C# MVP]

              #7
              Re: Dynamic Casting

              Philipp Schumann <phil@mokka.org > wrote:[color=blue]
              > Who says it is my design? My design just wants to work with ANY scenario, so
              > whether or not I _personally_ prefer the convenience of cast operators
              > (which can mean a lot _less_ overload definitions for many functions)
              > doesn't matter here. That said, the Dummy class was a moniker for all the
              > unknown types the API will need to work with, rather than a smallish design
              > of my own which I can change as I (or you) wish...
              >
              > The whole issue actually makes me wonder whether type conversions are C#
              > syntactic sugar rather than supported at the framework level. Almost
              > certainly seems to be the case, will have to check that.[/color]

              You can use reflection to find members with special names - they may
              well help you. The operator is compiled into a method, just a special
              one...

              --
              Jon Skeet - <skeet@pobox.co m>
              Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

              If replying to the group, please do not mail me too

              Comment

              Working...