Obtaining c# type declaration

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

    Obtaining c# type declaration


    Hi all

    I am using reflection to obtain some FieldInfo objects, what I need to do
    is convert the .Net type to the c# syntax - I can do this myself using my
    own conversion but I was wondering if there was a quick and easy way - example:

    Int32 would become int
    String would be come string

    Any suggestions? Should I just write my own?

    Thanks
    Kev


  • Jon Skeet [C# MVP]

    #2
    Re: Obtaining c# type declaration

    Lawrence Kevin wrote:[color=blue]
    > I am using reflection to obtain some FieldInfo objects, what I need to do
    > is convert the .Net type to the c# syntax - I can do this myself using my
    > own conversion but I was wondering if there was a quick and easy way - example:
    >
    > Int32 would become int
    > String would be come string
    >
    > Any suggestions? Should I just write my own?[/color]

    Yes, I don't think there's anything in the framework to do this for
    you. Just have a map from Type to name, and use map[typeof(int)]="int";
    etc. Then check for the presence of the type within the map, and use
    Type.Name if it's not there. There aren't many to do, fortunately.

    Jon

    Comment

    • Lawrence Kevin

      #3
      Re: Obtaining c# type declaration

      > Lawrence Kevin wrote:[color=blue]
      >[color=green]
      >> I am using reflection to obtain some FieldInfo objects, what I need
      >> to do is convert the .Net type to the c# syntax - I can do this
      >> myself using my own conversion but I was wondering if there was a
      >> quick and easy way - example:
      >>
      >> Int32 would become int
      >> String would be come string
      >> Any suggestions? Should I just write my own?
      >>[/color]
      > Yes, I don't think there's anything in the framework to do this for
      > you. Just have a map from Type to name, and use
      > map[typeof(int)]="int"; etc. Then check for the presence of the type
      > within the map, and use Type.Name if it's not there. There aren't many
      > to do, fortunately.
      >
      > Jon
      >[/color]

      Thanks

      Kev


      Comment

      • Ranjan

        #4
        Re: Obtaining c# type declaration

        Hello Lawrence,

        No in built functionality to get type specific "primitive type name". Anyway,
        there should not be any problem/issues with the ones you get using Reflection.
        Otherwise, you might have to write a switch to convert BCL names to language
        specific names.

        r.

        [color=blue]
        > Hi all
        >
        > I am using reflection to obtain some FieldInfo objects, what I need to
        > do is convert the .Net type to the c# syntax - I can do this myself
        > using my own conversion but I was wondering if there was a quick and
        > easy way - example:
        >
        > Int32 would become int
        > String would be come string
        > Any suggestions? Should I just write my own?
        >
        > Thanks
        > Kev[/color]


        Comment

        • Nicholas Paldino [.NET/C# MVP]

          #5
          Re: Obtaining c# type declaration

          Kev,

          I don't understand what the difference is really, since C# will accept
          String for "string" and Int32 for "int". They are aliases, and they don't
          exclude the type names as they are defined in the framework.


          --
          - Nicholas Paldino [.NET/C# MVP]
          - mvp@spam.guard. caspershouse.co m

          "Lawrence Kevin" <spamthis@spam. com> wrote in message
          news:3a030fc39b 7a8c7ebc9aeb4f6 c3@news.rmplc.c o.uk...[color=blue]
          >
          > Hi all
          >
          > I am using reflection to obtain some FieldInfo objects, what I need to do
          > is convert the .Net type to the c# syntax - I can do this myself using my
          > own conversion but I was wondering if there was a quick and easy way -
          > example:
          >
          > Int32 would become int
          > String would be come string
          >
          > Any suggestions? Should I just write my own?
          >
          > Thanks
          > Kev
          >
          >[/color]


          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: Obtaining c# type declaration

            Nicholas Paldino [.NET/C# MVP] <mvp@spam.guard .caspershouse.c om> wrote:[color=blue]
            > I don't understand what the difference is really, since C# will accept
            > String for "string" and Int32 for "int". They are aliases, and they don't
            > exclude the type names as they are defined in the framework.[/color]

            Sure - but I'm guessing that this could be used for a code-generator or
            something similar (documentation system?), where it would produce more
            idiomatic C# code if it used "int" instead of "Int32" etc.

            --
            Jon Skeet - <skeet@pobox.co m>
            http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
            If replying to the group, please do not mail me too

            Comment

            • Nicholas Paldino [.NET/C# MVP]

              #7
              Re: Obtaining c# type declaration

              Well, a statement could be made about one who doesn't know the
              difference between Int32 and int... =)


              --
              - Nicholas Paldino [.NET/C# MVP]
              - mvp@spam.guard. caspershouse.co m

              "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
              news:MPG.1e3c8a ed7edadeae98cd3 f@msnews.micros oft.com...[color=blue]
              > Nicholas Paldino [.NET/C# MVP] <mvp@spam.guard .caspershouse.c om> wrote:[color=green]
              >> I don't understand what the difference is really, since C# will
              >> accept
              >> String for "string" and Int32 for "int". They are aliases, and they
              >> don't
              >> exclude the type names as they are defined in the framework.[/color]
              >
              > Sure - but I'm guessing that this could be used for a code-generator or
              > something similar (documentation system?), where it would produce more
              > idiomatic C# code if it used "int" instead of "Int32" etc.
              >
              > --
              > Jon Skeet - <skeet@pobox.co m>
              > http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
              > If replying to the group, please do not mail me too[/color]


              Comment

              • Jon Skeet [C# MVP]

                #8
                Re: Obtaining c# type declaration

                Nicholas Paldino [.NET/C# MVP] <mvp@spam.guard .caspershouse.c om> wrote:[color=blue]
                > Well, a statement could be made about one who doesn't know the
                > difference between Int32 and int... =)[/color]

                Why should it be a matter of knowing differences? Why can't it be a
                simple matter of preference and familiarity? The MSDN library gives
                declarations using the C# aliases (in the details of the method), and I
                believe it's a better product because of that.

                --
                Jon Skeet - <skeet@pobox.co m>
                http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
                If replying to the group, please do not mail me too

                Comment

                • Nicholas Paldino [.NET/C# MVP]

                  #9
                  Re: Obtaining c# type declaration

                  In the general sense, I couldn't care less, it's all the same to me.

                  However, when dealing with the code generators, it's a little different,
                  since you would have to have an option (maybe one exists already, I don't
                  know, I haven't looked that much into it) to indicate that you should output
                  aliases.


                  --
                  - Nicholas Paldino [.NET/C# MVP]
                  - mvp@spam.guard. caspershouse.co m

                  "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
                  news:MPG.1e3c98 337246c6c298cd4 1@msnews.micros oft.com...[color=blue]
                  > Nicholas Paldino [.NET/C# MVP] <mvp@spam.guard .caspershouse.c om> wrote:[color=green]
                  >> Well, a statement could be made about one who doesn't know the
                  >> difference between Int32 and int... =)[/color]
                  >
                  > Why should it be a matter of knowing differences? Why can't it be a
                  > simple matter of preference and familiarity? The MSDN library gives
                  > declarations using the C# aliases (in the details of the method), and I
                  > believe it's a better product because of that.
                  >
                  > --
                  > Jon Skeet - <skeet@pobox.co m>
                  > http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
                  > If replying to the group, please do not mail me too[/color]


                  Comment

                  • Kevin Lawrence

                    #10
                    Re: Obtaining c# type declaration

                    I'm sticking to the C# keywords purely to follow coding-standards within
                    our department.

                    Kev
                    [color=blue]
                    > Kev,
                    >
                    > I don't understand what the difference is really, since C# will
                    > accept String for "string" and Int32 for "int". They are aliases, and
                    > they don't exclude the type names as they are defined in the
                    > framework.
                    >
                    > "Lawrence Kevin" <spamthis@spam. com> wrote in message
                    > news:3a030fc39b 7a8c7ebc9aeb4f6 c3@news.rmplc.c o.uk...
                    >[color=green]
                    >> Hi all
                    >>
                    >> I am using reflection to obtain some FieldInfo objects, what I need
                    >> to do is convert the .Net type to the c# syntax - I can do this
                    >> myself using my own conversion but I was wondering if there was a
                    >> quick and easy way - example:
                    >>
                    >> Int32 would become int
                    >> String would be come string
                    >> Any suggestions? Should I just write my own?
                    >>
                    >> Thanks
                    >> Kev[/color][/color]


                    Comment

                    Working...