Covert to typeB from typeA

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Javier Ros

    Covert to typeB from typeA

    hi guys,

    someone know if exists a function that convert a object from any type to
    other type?

    exaple:
    ConvertFromTO(o bject obj, System.Type typeSrc, System.Type typeDst);


  • Jon Skeet

    #2
    Re: Covert to typeB from typeA

    Javier Ros <jros@NO$PAMHER Eemuasa.es> wrote:[color=blue]
    > someone know if exists a function that convert a object from any type to
    > other type?
    >
    > exaple:
    > ConvertFromTO(o bject obj, System.Type typeSrc, System.Type typeDst);[/color]

    What would you expect it to do if presented with, say, Random and
    XmlDocument? Or WebRequest and Int32? Most conversions just don't make
    sense. If they make sense, they should be supported by one or other of
    the types (possibly using IConvertible).

    --
    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

    • Chuck Haeberle

      #3
      Re: Covert to typeB from typeA

      And if they do make sense but don't exist, you can usually create a derived
      class with an implicit or exlpicit conversion and implement the change
      yourself.

      "Jon Skeet" <skeet@pobox.co m> wrote in message
      news:MPG.1978c0 d823628d598a0b8 @news.microsoft .com...[color=blue]
      > Javier Ros <jros@NO$PAMHER Eemuasa.es> wrote:[color=green]
      > > someone know if exists a function that convert a object from any type[/color][/color]
      to[color=blue][color=green]
      > > other type?
      > >
      > > exaple:
      > > ConvertFromTO(o bject obj, System.Type typeSrc, System.Type typeDst);[/color]
      >
      > What would you expect it to do if presented with, say, Random and
      > XmlDocument? Or WebRequest and Int32? Most conversions just don't make
      > sense. If they make sense, they should be supported by one or other of
      > the types (possibly using IConvertible).
      >
      > --
      > Jon Skeet - <skeet@pobox.co m>
      > http://www.pobox.com/~skeet/
      > If replying to the group, please do not mail me too[/color]


      Comment

      • Maciej Kromrych

        #4
        Re: Covert to typeB from typeA

        "Javier Ros" <jros@NO$PAMHER Eemuasa.es> wrote in message
        news:e8Yirf6RDH A.2236@tk2msftn gp13.phx.gbl...[color=blue]
        > hi guys,
        >
        > someone know if exists a function that convert a object from any type to
        > other type?
        >
        > exaple:
        > ConvertFromTO(o bject obj, System.Type typeSrc, System.Type typeDst);[/color]

        Check ChangeType method of Convert class.

        HTH,
        Maciej


        Comment

        Working...