473,397 Members | 2,077 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,397 software developers and data experts.

How do I get the namespace of a object

I have a couple of WebForms that all inherit from a custom base class
(which in turn inherits from System.Web.UI.Page). I would like to
locate a function in the base class that iterates through the control
collection of the child page, looks at the type (looking for
System.Web.UI.WebControls.Label type), and pulls out the full
namespace and ID for that control (then goes out to a resouce file and
sets the text of the control for the specified locale amoungst some
other things). Everything is pretty simplistic except I cant figure
out how to extract the object namespace from the control (if that
information is even retained in the assembly).

In other words, I need to extract the namespace of a specific member
(NOT the namespace of the member "type"). I am looking for something
like "Company.Product.Module.SubModule.lblStreetAddress 3".

Below is some sample code. Thanks for your help!

Solomon Shaffer

Child WebForm:
namespace Company.Product.Module.SubModule
{
/// <summary>
/// Summary description for CreateAddress .
/// </summary>
public class CreateAddress : Page
{
protected System.Web.UI.WebControls.Label lblStreetAddress1;
protected System.Web.UI.WebControls.Label lblStreetAddress2;
protected System.Web.UI.WebControls.Label lblStreetAddress3;
protected System.Web.UI.WebControls.Label lblCity;
protected System.Web.UI.WebControls.Label lblState;
protected System.Web.UI.WebControls.Label lblZipCode;

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}
}
}

Base class:
namespace Company.Product.Module
{
/// <summary>
/// Summary description for Page.
/// </summary>
public class Page : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
System.Web.UI.Page oPage = (System.Web.UI.Page)sender;
SetControlText( oPage );
}

private void SetControlText( Control oParentControl )
{
ResourceManager oResourceManager = new ResourceManager( "English",
typeof( Page ).Assembly );

if ( oParentControl.HasControls() )
{
foreach ( Control oControl in oParentControl.Controls )
{
if ( oControl.GetType().ToString() ==
"System.Web.UI.WebControls.Label" )
{
Label oLabel = (Label)oControl;
//This does not work!
string sNamespace = oControl.GetType().Namespace.ToString();
oLabel.Text = oResourceManager.GetString( sNamespace + "." +
oControl.ID );
}

SetControlText( oControl );
}
}
}
}
}

Resource File:
<data name="Company.Product.Module.SubModule.lblStreetAd dress3">
<value>Address Line 3</value>
</data>
Nov 15 '05 #1
1 1577
If you can find the namespace of a type then you could find the page that the label is on and get the namespace of that.

--
Michael Culley
"Solomon Shaffer" <so*************@cynthetic.com> wrote in message news:a2**************************@posting.google.c om...
I have a couple of WebForms that all inherit from a custom base class
(which in turn inherits from System.Web.UI.Page). I would like to
locate a function in the base class that iterates through the control
collection of the child page, looks at the type (looking for
System.Web.UI.WebControls.Label type), and pulls out the full
namespace and ID for that control (then goes out to a resouce file and
sets the text of the control for the specified locale amoungst some
other things). Everything is pretty simplistic except I cant figure
out how to extract the object namespace from the control (if that
information is even retained in the assembly).

In other words, I need to extract the namespace of a specific member
(NOT the namespace of the member "type"). I am looking for something
like "Company.Product.Module.SubModule.lblStreetAddress 3".

Below is some sample code. Thanks for your help!

Solomon Shaffer

Child WebForm:
namespace Company.Product.Module.SubModule
{
/// <summary>
/// Summary description for CreateAddress .
/// </summary>
public class CreateAddress : Page
{
protected System.Web.UI.WebControls.Label lblStreetAddress1;
protected System.Web.UI.WebControls.Label lblStreetAddress2;
protected System.Web.UI.WebControls.Label lblStreetAddress3;
protected System.Web.UI.WebControls.Label lblCity;
protected System.Web.UI.WebControls.Label lblState;
protected System.Web.UI.WebControls.Label lblZipCode;

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}
}
}

Base class:
namespace Company.Product.Module
{
/// <summary>
/// Summary description for Page.
/// </summary>
public class Page : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
System.Web.UI.Page oPage = (System.Web.UI.Page)sender;
SetControlText( oPage );
}

private void SetControlText( Control oParentControl )
{
ResourceManager oResourceManager = new ResourceManager( "English",
typeof( Page ).Assembly );

if ( oParentControl.HasControls() )
{
foreach ( Control oControl in oParentControl.Controls )
{
if ( oControl.GetType().ToString() ==
"System.Web.UI.WebControls.Label" )
{
Label oLabel = (Label)oControl;
//This does not work!
string sNamespace = oControl.GetType().Namespace.ToString();
oLabel.Text = oResourceManager.GetString( sNamespace + "." +
oControl.ID );
}

SetControlText( oControl );
}
}
}
}
}

Resource File:
<data name="Company.Product.Module.SubModule.lblStreetAd dress3">
<value>Address Line 3</value>
</data>

Nov 15 '05 #2

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

Similar topics

88
by: Tim Tyler | last post by:
PHP puts most of its functions into a big flat global namespace. That leads to short function names - but creates a namespace minefield for programmers. Lots of the functions are legacies from...
2
by: Fritz Bosch | last post by:
Hi experts Is is possible to import/manipulate a module such that I can supply its __dict__? I want to supply my own dict subclass object to be filled by the import, e.g. a class like: >>>...
6
by: clinton__bill | last post by:
Hi, I usually use "using namespace <namespace_name>" to reference a namespace. Today I run across a code, in its header file it has this, namespace SP1{ class C1; } While SP1::C1 is a...
7
by: Kevin Newman | last post by:
I've been toying with a namespace manager, and wanted to get some input. So what do you think? if (typeof com == 'undefined') var com = {}; if (!com.unFocus) com.unFocus = {}; ...
27
by: Ron Adam | last post by:
Hi, I found the following to be a useful way to access arguments after they are passed to a function that collects them with **kwds. class namespace(dict): def __getattr__(self, name): return...
20
by: Patrick Guio | last post by:
Dear all, I have some problem with insertion operator together with namespace. I have a header file foo.h containing declaration of classes, typedefs and insertion operators for the typedefs in...
5
by: SenthilSS | last post by:
My application produces XML Data files which have XML namespace qualified XML elements (nodes), but the namespace itself is not declared in the data file. My task is to read these data files in a...
2
by: Pathogenix | last post by:
Greetings, I'm trying to fix a web service client which has been implemented in a dailywtf worthy manner. I've decided to rip all the old code out and start again from the proxy generated by...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.