How do I get the namespace of a object

S

Solomon Shaffer

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.lblStreetAddress3".

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.lblStreetAddress3">
<value>Address Line 3</value>
</data>
 
M

Michael Culley

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top