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. General Programming
  3. C#
  4. Using directive

Using directive

Scheduled Pinned Locked Moved C#
csharphelptutorialquestion
14 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.
  • A Offline
    A Offline
    Albu Marius
    wrote on last edited by
    #1

    I am working on a program that generates a C# class (with constructors and properties) by giving its members and their types. My problem is how can I find out what using directive to write at the beginning of the file so that the class file will compile even if , for example, the user gives as an input an Arraylist member.

    L J P 3 Replies Last reply
    0
    • A Albu Marius

      I am working on a program that generates a C# class (with constructors and properties) by giving its members and their types. My problem is how can I find out what using directive to write at the beginning of the file so that the class file will compile even if , for example, the user gives as an input an Arraylist member.

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      The simple answer is: you can not do that. If I ask for a Timer, how could you possibly know whether I want - a Windows.Forms.Timer - a Threading.Timer - a Timers.Timer all of these are standard classes in .NET Apart from that, you could either search all the system dll's using reflection (that will take way too much time), or include a little Dictionary that you populate by programmatically adding those entries that you consider important, and interact with the user about everything else. :)

      Luc Pattyn


      try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


      A 1 Reply Last reply
      0
      • L Luc Pattyn

        The simple answer is: you can not do that. If I ask for a Timer, how could you possibly know whether I want - a Windows.Forms.Timer - a Threading.Timer - a Timers.Timer all of these are standard classes in .NET Apart from that, you could either search all the system dll's using reflection (that will take way too much time), or include a little Dictionary that you populate by programmatically adding those entries that you consider important, and interact with the user about everything else. :)

        Luc Pattyn


        try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


        A Offline
        A Offline
        Albu Marius
        wrote on last edited by
        #3

        Thank you for your answer. I guess the dictionary solution is the only one I have :sigh: to address this issue. In case a user wants to add a type of class that is not contained in my collection I will let him/her to add the appropiate namespace to the dictionary .Is there a faster way than reflection to find out if the namespace is correct?

        L 1 Reply Last reply
        0
        • A Albu Marius

          Thank you for your answer. I guess the dictionary solution is the only one I have :sigh: to address this issue. In case a user wants to add a type of class that is not contained in my collection I will let him/her to add the appropiate namespace to the dictionary .Is there a faster way than reflection to find out if the namespace is correct?

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          Is there a faster way than reflection to find out if the namespace is correct? Not as far as I know. And it would be expensive: you would have to create a separate AppDomain I guess, then try to list all DLL candidates, load them (either all at once, exhausting memory, or one at the time; removing it is only possible by unloading the AppDomain). Warning: even the Dictionary approach is just an approximation; if there were only one Timer (say Timers.Timer) and you added it to the dictionary, now the next .NET release adds a second Timer (say Threading.Timer), your dictionary would not know it, and enforce one kind of Timer, whereas the user might want another one. So my guess is: 1) you should not even try to do the massive reflection at run-time (you might do it once to generate a dictionary) 2) I would generate a very small dictionary manually; it suffices to recognize a couple of the most popular classes for each DLL (e.g. File, FileStream, FileInfo, Directory is all I would recognize to include System.IO), there is no need to have an exhaustive list 3) whatever you do, it will only be an attempt, good enough to provide an initial source file, not good enough for generating a ready-made and error-free source file. BTW: Actually, when I said Dictionary, that is not strictly correct, since classnames are not unique (Timer example again). :)

          Luc Pattyn


          try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


          A 2 Replies Last reply
          0
          • L Luc Pattyn

            Is there a faster way than reflection to find out if the namespace is correct? Not as far as I know. And it would be expensive: you would have to create a separate AppDomain I guess, then try to list all DLL candidates, load them (either all at once, exhausting memory, or one at the time; removing it is only possible by unloading the AppDomain). Warning: even the Dictionary approach is just an approximation; if there were only one Timer (say Timers.Timer) and you added it to the dictionary, now the next .NET release adds a second Timer (say Threading.Timer), your dictionary would not know it, and enforce one kind of Timer, whereas the user might want another one. So my guess is: 1) you should not even try to do the massive reflection at run-time (you might do it once to generate a dictionary) 2) I would generate a very small dictionary manually; it suffices to recognize a couple of the most popular classes for each DLL (e.g. File, FileStream, FileInfo, Directory is all I would recognize to include System.IO), there is no need to have an exhaustive list 3) whatever you do, it will only be an attempt, good enough to provide an initial source file, not good enough for generating a ready-made and error-free source file. BTW: Actually, when I said Dictionary, that is not strictly correct, since classnames are not unique (Timer example again). :)

            Luc Pattyn


            try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


            A Offline
            A Offline
            Albu Marius
            wrote on last edited by
            #5

            That's what I was affraid off....:((

            1 Reply Last reply
            0
            • L Luc Pattyn

              Is there a faster way than reflection to find out if the namespace is correct? Not as far as I know. And it would be expensive: you would have to create a separate AppDomain I guess, then try to list all DLL candidates, load them (either all at once, exhausting memory, or one at the time; removing it is only possible by unloading the AppDomain). Warning: even the Dictionary approach is just an approximation; if there were only one Timer (say Timers.Timer) and you added it to the dictionary, now the next .NET release adds a second Timer (say Threading.Timer), your dictionary would not know it, and enforce one kind of Timer, whereas the user might want another one. So my guess is: 1) you should not even try to do the massive reflection at run-time (you might do it once to generate a dictionary) 2) I would generate a very small dictionary manually; it suffices to recognize a couple of the most popular classes for each DLL (e.g. File, FileStream, FileInfo, Directory is all I would recognize to include System.IO), there is no need to have an exhaustive list 3) whatever you do, it will only be an attempt, good enough to provide an initial source file, not good enough for generating a ready-made and error-free source file. BTW: Actually, when I said Dictionary, that is not strictly correct, since classnames are not unique (Timer example again). :)

              Luc Pattyn


              try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


              A Offline
              A Offline
              Albu Marius
              wrote on last edited by
              #6

              I have an idea. I don't know if it is a good one but here it goes... What if I try to instantiate an object of that type inside a using directive (using the namespace provided), catch the exception if it appears and notify the user...or by using System.Type.GetType() in some way...:~

              L 1 Reply Last reply
              0
              • A Albu Marius

                I have an idea. I don't know if it is a good one but here it goes... What if I try to instantiate an object of that type inside a using directive (using the namespace provided), catch the exception if it appears and notify the user...or by using System.Type.GetType() in some way...:~

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #7

                There are only two ways to instantiate a class: - the normal way, with the new keyword; it implies you have compiled correct code hence you already have the necessary using statement (and the reference in your project); - the reflective way, which means you load some assembly, locate the class, and invoke its constructor; hence you must know which assembly to load; if you do, you also know which using is required ! So the only thing that you can do is collect the source lines, add using statements as much as you see fit, then try to compile and present (a summary of) the error messages to the user. :)

                Luc Pattyn


                try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


                A 1 Reply Last reply
                0
                • L Luc Pattyn

                  There are only two ways to instantiate a class: - the normal way, with the new keyword; it implies you have compiled correct code hence you already have the necessary using statement (and the reference in your project); - the reflective way, which means you load some assembly, locate the class, and invoke its constructor; hence you must know which assembly to load; if you do, you also know which using is required ! So the only thing that you can do is collect the source lines, add using statements as much as you see fit, then try to compile and present (a summary of) the error messages to the user. :)

                  Luc Pattyn


                  try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


                  A Offline
                  A Offline
                  Albu Marius
                  wrote on last edited by
                  #8

                  Ehhh..I was worth the shot :)

                  1 Reply Last reply
                  0
                  • A Albu Marius

                    I am working on a program that generates a C# class (with constructors and properties) by giving its members and their types. My problem is how can I find out what using directive to write at the beginning of the file so that the class file will compile even if , for example, the user gives as an input an Arraylist member.

                    J Offline
                    J Offline
                    J4amieC
                    wrote on last edited by
                    #9

                    If the user is specifying the members/methods/properties why not also let them specify the Using directives to use? Or alterntively make them fully specify any types used (in this case System.Collections.ArrayList) which would negate the need to have using directives at all.

                    --- How to get answers to your questions[^]

                    1 Reply Last reply
                    0
                    • A Albu Marius

                      I am working on a program that generates a C# class (with constructors and properties) by giving its members and their types. My problem is how can I find out what using directive to write at the beginning of the file so that the class file will compile even if , for example, the user gives as an input an Arraylist member.

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

                      Don't use using directives.

                      A 1 Reply Last reply
                      0
                      • P PIEBALDconsult

                        Don't use using directives.

                        A Offline
                        A Offline
                        Albu Marius
                        wrote on last edited by
                        #11

                        I need using directives because I want to give my newly created class that is included in a given project a chance that it will compile. I don't want to let the user crash a good project with some typos or something. I think I will go with the suggestion of forcing the user to select a given type. Probably I will implement somekind of intellisense.

                        P 1 Reply Last reply
                        0
                        • A Albu Marius

                          I need using directives because I want to give my newly created class that is included in a given project a chance that it will compile. I don't want to let the user crash a good project with some typos or something. I think I will go with the suggestion of forcing the user to select a given type. Probably I will implement somekind of intellisense.

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

                          You never need using directives; they are crutches for weak programmers.

                          A 1 Reply Last reply
                          0
                          • P PIEBALDconsult

                            You never need using directives; they are crutches for weak programmers.

                            A Offline
                            A Offline
                            Albu Marius
                            wrote on last edited by
                            #13

                            PIEBALDconsult I don't think you understant what am I doing. I am generating a C# code file (*.cs) row by row. Of course I need using directives when I start writing like, for example "using System;". I don't intend to write using directives in any of the methods code.

                            P 1 Reply Last reply
                            0
                            • A Albu Marius

                              PIEBALDconsult I don't think you understant what am I doing. I am generating a C# code file (*.cs) row by row. Of course I need using directives when I start writing like, for example "using System;". I don't intend to write using directives in any of the methods code.

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

                              Right, don't do that, write out the full name of everything.

                              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