interlocking namespaces

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

    #1

    interlocking namespaces

    Hi,
    The namespace property of Type class returns the namespace of a given type.
    is there any function to test that a namespace is nested within an other
    namespace ?
    Thanks in advance.


  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: interlocking namespaces

    Abdessamad,

    Not really. In order to do that, you will have to do some string
    comparison on your own. It shouldn't be hard though, given that namespaces
    are period-delimited.

    Hope this helps.


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

    "Abdessamad Belangour" <belangour@irin .univ-nantes.fr> wrote in message
    news:OTdVwLDtDH A.2456@TK2MSFTN GP12.phx.gbl...[color=blue]
    > Hi,
    > The namespace property of Type class returns the namespace of a given[/color]
    type.[color=blue]
    > is there any function to test that a namespace is nested within an other
    > namespace ?
    > Thanks in advance.
    >
    >[/color]


    Comment

    • Mickey Williams

      #3
      Re: interlocking namespaces

      In most of the CLR, namespaces are fiction, and the name of the type
      actually includes the namespace (some .NET languages don't understand
      namespaces). Looking specifically at the Type class, the Namespace property
      returns the entire type name (including the C# namespace), minus the (C#)
      name of the type, so given:

      namespace f.o.o.b.a.r
      {
      public class X { }
      }

      which is the same thing as:

      namespace f
      {
      namespace o
      {
      namespace o
      {
      namespace b
      {
      namespace a
      {
      namespace r
      {
      public class X { }
      }
      }
      }
      }
      }
      }

      When referring to typeof(X), the Namespace property will return:
      "f.o.o.b.a. r"

      --
      Mickey Williams
      Author, "Microsoft Visual C# .NET Core Reference", MS Press



      "Abdessamad Belangour" <belangour@irin .univ-nantes.fr> wrote in message
      news:OTdVwLDtDH A.2456@TK2MSFTN GP12.phx.gbl...[color=blue]
      > Hi,
      > The namespace property of Type class returns the namespace of a given[/color]
      type.[color=blue]
      > is there any function to test that a namespace is nested within an other
      > namespace ?
      > Thanks in advance.
      >
      >[/color]


      Comment

      • Petter

        #4
        Re: interlocking namespaces

        If you got a class like this

        namespace MyOuterNamespac e
        {
        namespace MyInnerNamespac e
        {
        class MyClass
        {
        }
        }
        }

        then the Type.Namespace property returns the string
        "MyOuterNamespa ce.MyInnerNames pace". Using this (and some string
        manipulation) you should be able to "parse" out which namespace(s) the
        type/class is nested within.

        Petter



        "Abdessamad Belangour" <belangour@irin .univ-nantes.fr> wrote in message
        news:OTdVwLDtDH A.2456@TK2MSFTN GP12.phx.gbl...[color=blue]
        > Hi,
        > The namespace property of Type class returns the namespace of a given[/color]
        type.[color=blue]
        > is there any function to test that a namespace is nested within an other
        > namespace ?
        > Thanks in advance.
        >
        >[/color]


        Comment

        • Zhanyong Wan [MSFT]

          #5
          Re: interlocking namespaces

          -------------------[color=blue]
          > From: "Petter" <wildfrog@NOSPA Mspray.no>
          > Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
          >
          > If you got a class like this
          >
          > namespace MyOuterNamespac e
          > {
          > namespace MyInnerNamespac e
          > {
          > class MyClass
          > {
          > }
          > }
          > }
          >
          > then the Type.Namespace property returns the string
          > "MyOuterNamespa ce.MyInnerNames pace". Using this (and some string
          > manipulation) you should be able to "parse" out which namespace(s) the
          > type/class is nested within.
          >
          > Petter
          >
          >
          >
          > "Abdessamad Belangour" <belangour@irin .univ-nantes.fr> wrote in message
          > news:OTdVwLDtDH A.2456@TK2MSFTN GP12.phx.gbl...[color=green]
          > > Hi,
          > > The namespace property of Type class returns the namespace of a given[/color]
          > type.[color=green]
          > > is there any function to test that a namespace is nested within an other
          > > namespace ?
          > > Thanks in advance.[/color][/color]

          Petter is right.

          In case you are still wondering what kind of string manipulation is needed,
          the StartsWith() method in the String class is useful. However, if you
          have two namespace strings ns1 and ns2,

          ns1.StartsWith( ns2 )

          will NOT always tell you whether ns1 is nested in ns2. For example, when
          ns1 is "Foo.Bar" and ns2 is "Foo.B", the above expression returns true
          while ns1 is not really nested in ns2.

          Given that namespaces are period-separated, the following trick will solve
          the problem:

          (ns1 + ".").StartsWith ( ns2 + ".")

          HTH,

          - Zhanyong Wan

          Visual Studio and .NET Setup

          This posting is provided "AS IS" with no warranties, and confers no rights.
          Use of included samples (if any) is subject to the terms specified at
          http://www.microsoft.com/info/cpyright.htm

          Comment

          • Jens Thiel

            #6
            Re: interlocking namespaces


            "Zhanyong Wan [MSFT]" <zhanyw@online. microsoft.com> wrote[color=blue]
            >
            > Given that namespaces are period-separated, the following trick will solve
            > the problem:
            >
            > (ns1 + ".").StartsWith ( ns2 + ".")
            >[/color]

            Actually,

            ns1.StartsWith( ns2 + ".")

            should be sufficient to test for a nested namespace ;-)

            Jens.


            --
            http://ManagedXLL.net/ | http://jens-thiel.de/ | http://QuantLib.net/
            Replace MSDN with my first name when replying to my email address!


            Comment

            Working...