Generic filehandler method

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

    Generic filehandler method

    I am trying to build a generic static method that will return a specified
    type from a method. My problem is that I need to return say an image from
    the method...and I'm having trouble understanding how to do this.

    This is what I have so far...(The new Image is hardcoded right now...I would
    like to abstract that out)

    private T GetFileFromFile System<T>(strin g filePath, string
    fileNameWithout Extension)
    {
    string tempExtension = "";

    if (Directory.Exis ts(filePath))
    {
    if (Path.GetFileNa meWithoutExtens ion(filePath + "\\" +
    imageNameWithou tExtension).ToS tring().Trim(). Length 0)
    {
    tempExtension = Path.GetExtensi on(filePath + "\\" +
    imageNameWithou tExtension);
    return (T)(Object)new Bitmap(filePath + "\\" +
    imageNameWithou tExtension + "." + tempExtension);
    }
    else
    {
    return default(T);
    }
    }
    }

    Thanks for any help you might be able to provide,
    Ron

  • mrshrinkray@googlemail.com

    #2
    Re: Generic filehandler method

    On 15 May, 14:23, "Ron" <rs_herh...@yah oo.comwrote:
    I am trying to build a generic static method that will return a specified
    type from a method. My problem is that I need to return say an image from
    the method...and I'm having trouble understanding how to do this.
    >
    This is what I have so far...(The new Image is hardcoded right now...I would
    like to abstract that out)
    >
    private T GetFileFromFile System<T>(strin g filePath, string
    fileNameWithout Extension)
    {
    string tempExtension = "";
    >
    if (Directory.Exis ts(filePath))
    {
    if (Path.GetFileNa meWithoutExtens ion(filePath + "\\" +
    imageNameWithou tExtension).ToS tring().Trim(). Length 0)
    {
    tempExtension = Path.GetExtensi on(filePath + "\\" +
    imageNameWithou tExtension);
    return (T)(Object)new Bitmap(filePath + "\\" +
    imageNameWithou tExtension + "." + tempExtension);
    }
    else
    {
    return default(T);
    }
    }
    }
    >
    Thanks for any help you might be able to provide,
    Ron
    What do you envisage T as being?

    Comment

    • Ron

      #3
      Re: Generic filehandler method

      Thank you for the reply,

      Text files, an image right now but I would like the flexibility for other
      uses in the future.

      Thanks,
      Ron


      <mrshrinkray@go oglemail.comwro te in message
      news:48a9d390-8bf4-4728-b4aa-ba09e588bdb3@d7 7g2000hsb.googl egroups.com...
      On 15 May, 14:23, "Ron" <rs_herh...@yah oo.comwrote:
      >I am trying to build a generic static method that will return a specified
      >type from a method. My problem is that I need to return say an image
      >from
      >the method...and I'm having trouble understanding how to do this.
      >>
      >This is what I have so far...(The new Image is hardcoded right now...I
      >would
      >like to abstract that out)
      >>
      > private T GetFileFromFile System<T>(strin g filePath, string
      >fileNameWithou tExtension)
      > {
      > string tempExtension = "";
      >>
      > if (Directory.Exis ts(filePath))
      > {
      > if (Path.GetFileNa meWithoutExtens ion(filePath + "\\" +
      >imageNameWitho utExtension).To String().Trim() .Length 0)
      > {
      > tempExtension = Path.GetExtensi on(filePath + "\\" +
      >imageNameWitho utExtension);
      > return (T)(Object)new Bitmap(filePath + "\\" +
      >imageNameWitho utExtension + "." + tempExtension);
      > }
      > else
      > {
      > return default(T);
      > }
      > }
      > }
      >>
      >Thanks for any help you might be able to provide,
      >Ron
      >
      What do you envisage T as being?

      Comment

      • Peter Duniho

        #4
        Re: Generic filehandler method

        On Thu, 15 May 2008 06:23:27 -0700, Ron <rs_herhuth@yah oo.comwrote:
        I am trying to build a generic static method that will return a
        specified type from a method. My problem is that I need to return say
        an image from the method...and I'm having trouble understanding how to
        do this.
        It is not clear why you want this method to be generic. Whatever type you
        supply for T, it _must_ be valid for the type of data you expect to
        extract from the file. That means that in advance, you need to know what
        type you expect. That means that there's no need for a generic method to
        read the file. Just use existing type-specific i/o mechanisms.

        Pete

        Comment

        • mrshrinkray@googlemail.com

          #5
          Re: Generic filehandler method

          On 15 May, 16:15, "Peter Duniho" <NpOeStPe...@nn owslpianmk.comw rote:
          On Thu, 15 May 2008 06:23:27 -0700, Ron <rs_herh...@yah oo.comwrote:
          I am trying to build a generic static method that will return a
          specified type from a method. My problem is that I need to return say
          an image from the method...and I'm having trouble understanding how to
          do this.
          >
          It is not clear why you want this method to be generic. Whatever type you
          supply for T, it _must_ be valid for the type of data you expect to
          extract from the file. That means that in advance, you need to know what
          type you expect. That means that there's no need for a generic method to
          read the file. Just use existing type-specific i/o mechanisms.
          >
          Pete
          You would need to create your own wrapper classes around each type of
          file (ImageLoader,Te xtFileLoader,Bi naryFileLoad) which as pete said
          you might as well just make a class with 4 methods to handle each one,
          Image and FileStream have nothing in common besides Object so there's
          no way of calling a Load method generically

          Comment

          • Ron

            #6
            Re: Generic filehandler method

            The subtlety is that using this method the filetype is specified by the
            programmer that is coding the caller operation, and I would imagine there is
            some way to open a file as a type...is that not the case?

            Thanks,
            Ron



            <mrshrinkray@go oglemail.comwro te in message
            news:46c02ac3-6e52-4d91-ad4b-02477383d635@b6 4g2000hsa.googl egroups.com...
            On 15 May, 16:15, "Peter Duniho" <NpOeStPe...@nn owslpianmk.comw rote:
            >On Thu, 15 May 2008 06:23:27 -0700, Ron <rs_herh...@yah oo.comwrote:
            I am trying to build a generic static method that will return a
            specified type from a method. My problem is that I need to return say
            an image from the method...and I'm having trouble understanding how to
            do this.
            >>
            >It is not clear why you want this method to be generic. Whatever type
            >you
            >supply for T, it _must_ be valid for the type of data you expect to
            >extract from the file. That means that in advance, you need to know what
            >type you expect. That means that there's no need for a generic method to
            >read the file. Just use existing type-specific i/o mechanisms.
            >>
            >Pete
            >
            You would need to create your own wrapper classes around each type of
            file (ImageLoader,Te xtFileLoader,Bi naryFileLoad) which as pete said
            you might as well just make a class with 4 methods to handle each one,
            Image and FileStream have nothing in common besides Object so there's
            no way of calling a Load method generically

            Comment

            • mrshrinkray@googlemail.com

              #7
              Re: Generic filehandler method

              On 15 May, 16:32, "Ron" <rs_herh...@yah oo.comwrote:
              The subtlety is that using this method the filetype is specified by the
              programmer that is coding the caller operation, and I would imagine there is
              some way to open a file as a type...is that not the case?
              >
              Thanks,
              Ron
              But how would your static method know which method to call on the
              object to Load the file? And when you get T back, what common way
              would you use a Bitmap or a string?

              Comment

              • Ron

                #8
                Re: Generic filehandler method

                Something like this:

                <psuedo code>

                Image img = GetFileFromFile System<Image>(" C:\\Images\\"," Background.bmp" );

                - or -

                String str= GetFileFromFile System<String>( "C:\\Images\\", "DisplayText.tx t");






                <mrshrinkray@go oglemail.comwro te in message
                news:b54d2e51-4574-4246-9675-82c9741bb917@x4 1g2000hsb.googl egroups.com...
                On 15 May, 16:32, "Ron" <rs_herh...@yah oo.comwrote:
                >The subtlety is that using this method the filetype is specified by the
                >programmer that is coding the caller operation, and I would imagine there
                >is
                >some way to open a file as a type...is that not the case?
                >>
                >Thanks,
                >Ron
                >
                But how would your static method know which method to call on the
                object to Load the file? And when you get T back, what common way
                would you use a Bitmap or a string?

                Comment

                • Paul E Collins

                  #9
                  Re: Generic filehandler method

                  "Ron" <rs_herhuth@yah oo.comwrote:
                  The subtlety is that using this method the filetype is specified by
                  the programmer that is coding the caller operation, and I would
                  imagine there is some way to open a file as a type...is that not the
                  case?
                  That's not the case at all.

                  Eq.


                  Comment

                  • Peter Duniho

                    #10
                    Re: Generic filehandler method

                    On Thu, 15 May 2008 09:35:18 -0700, Ron <rs_herhuth@yah oo.comwrote:
                    Something like this:
                    >
                    <psuedo code>
                    >
                    Image img =
                    GetFileFromFile System<Image>(" C:\\Images\\"," Background.bmp" );
                    >
                    - or -
                    >
                    String str=
                    GetFileFromFile System<String>( "C:\\Images\\", "DisplayText.tx t");
                    But Image and String share absolutely nothing in common with respect to
                    how they might obtain an instance of said type from a file.

                    The point of generics is to allow you to write generic code that takes
                    advantage of commonalities in the type parameter, so that you don't have
                    to basically write the same code multiple times just to use it with
                    multiple types. But you're going to have very little code in your generic
                    method that is shared between the different types it will support, and
                    there's _no_ general-purpose way to turn an arbitrary file into a specific
                    type (other than serialization, which isn't what you're doing here), so
                    your "generic" method will necessarily have to contain special-case code
                    for every anticipated type parameter (and fail gracefully when used witha
                    type parameter that wasn't anticipated).

                    For Image, we already have Image.FromFile( ). For String, we already have
                    StreamReader(st ring) (constructing a StreamReader for a specific file) and
                    StreamReader.Re adToEnd() (returns a string that represents the contents of
                    the file). You can easily wrap the StreamReader functionality in an
                    extension method you name FromFile() if you like, and likewise any other
                    specific type you anticipate needing to deal with.

                    I really just don't see how a generic method is helping you here.

                    Pete

                    Comment

                    • Ron

                      #11
                      Re: Generic filehandler method

                      Okay waht about returning a File object from the method? So I can do all my
                      file exists logic and if it exists then return a file and let the calling
                      code do the processing?


                      "Paul E Collins" <find_my_real_a ddress@CL4.orgw rote in message
                      news:2_KdndPaur 699LHVRVnyjQA@b t.com...
                      "Ron" <rs_herhuth@yah oo.comwrote:
                      >
                      >The subtlety is that using this method the filetype is specified by the
                      >programmer that is coding the caller operation, and I would imagine there
                      >is some way to open a file as a type...is that not the case?
                      >
                      That's not the case at all.
                      >
                      Eq.
                      >
                      >

                      Comment

                      • Peter Duniho

                        #12
                        Re: Generic filehandler method

                        On Thu, 15 May 2008 10:03:46 -0700, Ron <rs_herhuth@yah oo.comwrote:
                        Okay waht about returning a File object from the method? So I can do
                        all my file exists logic and if it exists then return a file and let the
                        calling code do the processing?
                        All what "file exists logic"? What is there other than to call
                        File.Exists()?

                        Note: there's no such thing as a File object. That class is static. You
                        could return a FileInfo object, which would be useful if you expect to
                        perform multiple operations from that class using the same file. But
                        there's nothing about what you've written that suggests that might be the
                        case. All you really need is a filename, to use a file.

                        Pete

                        Comment

                        • Paul E Collins

                          #13
                          Re: Generic filehandler method

                          "Ron" <rs_herhuth@yah oo.comwrote:
                          Okay waht about returning a File object from the method? So I can do
                          all my file exists logic and if it exists then return a file and let
                          the calling code do the processing?
                          I think, rather than generics, inheritance might be a decent approach
                          here (because, as others have said, the different objects have nothing
                          in common and no direct mapping to a file type either).

                          You could have e.g. an abstract FileOpener base class (including the
                          common bits of logic, like checking for a file's existence) and then
                          override some method: object OpenFile(string path) so that the derived
                          classes can offer their own implementation (which might be
                          Image.FromFile, or File.ReadAllTex t, or whatever).

                          The main problem with this idea is that you have to return 'object', so
                          the caller has to cast the returned object to something useful (Image,
                          string, etc.). There are various tolerable workarounds, though.

                          Eq.


                          Comment

                          • mrshrinkray@googlemail.com

                            #14
                            Re: Generic filehandler method

                            On May 15, 5:35 pm, "Ron" <rs_herh...@yah oo.comwrote:
                            Something like this:
                            >
                            <psuedo code>
                            >
                            Image img = GetFileFromFile System<Image>(" C:\\Images\\"," Background.bmp" );
                            >
                            - or -
                            >
                            String str= GetFileFromFile System<String>( "C:\\Images\\", "DisplayText.tx t");
                            >
                            or

                            Image img = Image.LoadFromF ile(...);
                            string str = File.OpenText(. .)

                            Comment

                            • Ron

                              #15
                              Re: Generic filehandler method

                              Peter,

                              Thanks. I implemented your suggestion and that will work just fine.


                              "Peter Duniho" <NpOeStPeAdM@nn owslpianmk.comw rote in message
                              news:op.ua7hgyy e8jd0ej@petes-computer.local. ..
                              On Thu, 15 May 2008 10:03:46 -0700, Ron <rs_herhuth@yah oo.comwrote:
                              >
                              >Okay waht about returning a File object from the method? So I can do
                              >all my file exists logic and if it exists then return a file and let the
                              >calling code do the processing?
                              >
                              All what "file exists logic"? What is there other than to call
                              File.Exists()?
                              >
                              Note: there's no such thing as a File object. That class is static. You
                              could return a FileInfo object, which would be useful if you expect to
                              perform multiple operations from that class using the same file. But
                              there's nothing about what you've written that suggests that might be the
                              case. All you really need is a filename, to use a file.
                              >
                              Pete

                              Comment

                              Working...