473,386 Members | 1,743 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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.
Nov 15 '05 #1
5 1136
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]
- mv*@spam.guard.caspershouse.com

"Abdessamad Belangour" <be*******@irin.univ-nantes.fr> wrote in message
news:OT**************@TK2MSFTNGP12.phx.gbl...
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.

Nov 15 '05 #2
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
www.servergeek.com
"Abdessamad Belangour" <be*******@irin.univ-nantes.fr> wrote in message
news:OT**************@TK2MSFTNGP12.phx.gbl...
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.

Nov 15 '05 #3
If you got a class like this

namespace MyOuterNamespace
{
namespace MyInnerNamespace
{
class MyClass
{
}
}
}

then the Type.Namespace property returns the string
"MyOuterNamespace.MyInnerNamespace". 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" <be*******@irin.univ-nantes.fr> wrote in message
news:OT**************@TK2MSFTNGP12.phx.gbl...
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.

Nov 15 '05 #4
-------------------
From: "Petter" <wi******@NOSPAMspray.no>
Newsgroups: microsoft.public.dotnet.languages.csharp

If you got a class like this

namespace MyOuterNamespace
{
namespace MyInnerNamespace
{
class MyClass
{
}
}
}

then the Type.Namespace property returns the string
"MyOuterNamespace.MyInnerNamespace". 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" <be*******@irin.univ-nantes.fr> wrote in message
news:OT**************@TK2MSFTNGP12.phx.gbl...
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.


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

Nov 15 '05 #5

"Zhanyong Wan [MSFT]" <zh****@online.microsoft.com> wrote

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

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


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!
Nov 15 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Steven Bethard | last post by:
In the "empty classes as c structs?" thread, we've been talking in some detail about my proposed "generic objects" PEP. Based on a number of suggestions, I'm thinking more and more that instead of...
24
by: Marcin Vorbrodt | last post by:
Here is an example of my code: //Header file #include <vector> using std::vector; namespace Revelation { // class definitions, etc... // class members are of type std::vector }
17
by: clintonG | last post by:
Using 2.0 with Master Pages and a GlobalBaseClass for the content pages. I understand the easy part -- the hierarchical structure of a namespace naming convention -- but the 2.0 IDE does not...
11
by: Random | last post by:
I'm confused about the proper use and usefulness of namespaces. I beleive I understand the purpose is so the developer can put classes within namespaces to essentially organize your code. And I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.