Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. Tired of not having a generic version of IClonable?

Tired of not having a generic version of IClonable?

Scheduled Pinned Locked Moved The Weird and The Wonderful
questionadobearchitectureannouncement
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Brisingr Aerowing
    wrote on last edited by
    #1

    Try this extension method:

    public static T Clone<T>(this T obj) where T : ICloneable
    {
    return (T)(((ICloneable)obj).Clone());
    }

    In VB:

    <Extension()>
    Public Shared Function Clone(Of T As IClonable)(ByVal obj As T) As T
    Return DirectCast(DirectCast(obj, IClonable).Clone(), T)
    End Function

    (If the VB syntax is wrong, tell me. I don't use VB any more)

    What do you get when you cross a joke with a rhetorical question? The metaphorical solid rear-end expulsions have impacted the metaphorical motorized bladed rotating air movement mechanism. Do questions with multiple question marks annoy you???

    G P D 3 Replies Last reply
    0
    • B Brisingr Aerowing

      Try this extension method:

      public static T Clone<T>(this T obj) where T : ICloneable
      {
      return (T)(((ICloneable)obj).Clone());
      }

      In VB:

      <Extension()>
      Public Shared Function Clone(Of T As IClonable)(ByVal obj As T) As T
      Return DirectCast(DirectCast(obj, IClonable).Clone(), T)
      End Function

      (If the VB syntax is wrong, tell me. I don't use VB any more)

      What do you get when you cross a joke with a rhetorical question? The metaphorical solid rear-end expulsions have impacted the metaphorical motorized bladed rotating air movement mechanism. Do questions with multiple question marks annoy you???

      G Offline
      G Offline
      Garth J Lancaster
      wrote on last edited by
      #2

      neat !

      1 Reply Last reply
      0
      • B Brisingr Aerowing

        Try this extension method:

        public static T Clone<T>(this T obj) where T : ICloneable
        {
        return (T)(((ICloneable)obj).Clone());
        }

        In VB:

        <Extension()>
        Public Shared Function Clone(Of T As IClonable)(ByVal obj As T) As T
        Return DirectCast(DirectCast(obj, IClonable).Clone(), T)
        End Function

        (If the VB syntax is wrong, tell me. I don't use VB any more)

        What do you get when you cross a joke with a rhetorical question? The metaphorical solid rear-end expulsions have impacted the metaphorical motorized bladed rotating air movement mechanism. Do questions with multiple question marks annoy you???

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #3

        No, but I found this in my code today:

        public static T
        Clone<T>
        (
        this T Connection
        )
        where T : System.Data.IDbConnection , new()
        {
        return ( new T()
        {
        ConnectionString = Connection.ConnectionString
        } ) ;
        }

        I get the feeling that I never even tried to test it. I'm not sure I ever should. But it compiles! To make matters worse, I found that because I had just written this:

        public static partial class LibExt
        {
        private static readonly System.Collections.Generic.Dictionary<System.Type,System.Reflection.ConstructorInfo> constructors ;

        static LibExt
        (
        )
        {
        constructors = new System.Collections.Generic.Dictionary<System.Type,System.Reflection.ConstructorInfo>() ;

        return ;
        

        }

        public static System.Data.IDbConnection
        CloneConnection
        (
        this System.Data.IDbConnection Connection
        )
        {
        System.Data.IDbConnection result ;

        System.Type typ = Connection.GetType() ;
        
        if ( !constructors.ContainsKey ( typ ) )
        {
          constructors \[ typ \] = typ.GetConstructor ( System.Type.EmptyTypes ) ;
        }
        
        result = (System.Data.IDbConnection) constructors \[ typ \].Invoke ( new object \[ 0 \] ) ;
        
        result.ConnectionString = Connection.ConnectionString ;
        
        return ( result ) ;
        

        }
        }

        Which I haven't tested either. :sigh:

        1 Reply Last reply
        0
        • B Brisingr Aerowing

          Try this extension method:

          public static T Clone<T>(this T obj) where T : ICloneable
          {
          return (T)(((ICloneable)obj).Clone());
          }

          In VB:

          <Extension()>
          Public Shared Function Clone(Of T As IClonable)(ByVal obj As T) As T
          Return DirectCast(DirectCast(obj, IClonable).Clone(), T)
          End Function

          (If the VB syntax is wrong, tell me. I don't use VB any more)

          What do you get when you cross a joke with a rhetorical question? The metaphorical solid rear-end expulsions have impacted the metaphorical motorized bladed rotating air movement mechanism. Do questions with multiple question marks annoy you???

          D Offline
          D Offline
          Dennis_E
          wrote on last edited by
          #4

          Note that this only works if T has an explicit implementation of ICloneable. Otherwise, Clone() will call the instance method and not the extension method. And you don't need the cast to ICloneable.

          return (T)obj.Clone();

          will do.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups