Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Types and classes

Status
Not open for further replies.

rtb1

IS-IT--Management
Jul 17, 2000
73
ES
Hi,

I am new to c# and not sure how to do the following.

I have to namespaces. Namespace2 is compiled at runtime and always has a different name. The properties and methods are however known.

From namespace1 I want to be able to access instances of a class in namespace2.

To get a reference to this class I tried something like:
Code:
Type classType;
...

Assembly a = Assembly.Load("namespace2");
classType = a.GetType("namespace2.ClassName");

Then I have a method in class1 in namespace1 with a reference to the created classType that looks like
Code:
public void TestClass(classType testclass){
  ...
}

This returns the error:
"classType is a `field' but a `type' was expected"

Anyone knows what goes wrong or how to do this kind of thing?

Thanks!
 
rtb1,

Firstly, why are the namespaces changing from one compilation to another? The purpose of namespacing is to avoid naming conflicts. So for example:

1.) the System.Net.Sockets namespace contains a class called Socket.
2.) I want to write a class named Socket but I don't want there to be an ambiguous reference when the user types:
Socket s = new Socket(...);
3.) I make a new namespace named 'Phyrtech.Net.Sockets' and put my class into it so that a coder can reference either my Socket implementation or the System.Net.Sockets implementation.

If the namespace always remains the same, then we can do the following:

Code:
namespace Phyrtech.Net.Sockets
{
	public class Socket
	{
		public void Connect()
		{
			//connect to something
		}
	}
}

namespace TestProgram
{
	public static void Main()
	{
		//I can use Phyrtech.Net.Sockets.Socket
		Phyrtech.Net.Sockets.Socket s = new Phyrtech.Net.Sockets.Socket();

		s.DoSomething();

		//or I can use System.Net.Sockets.Socket
		System.Net.Sockets.Socket socket = new System.Net.Sockets.Socket(...);

		socket.Connect(...);
	}
}

Does this help?

Sincerely,
Nate

Phyrstorm Technologies, Inc.
 
Namespace2 is compiled at runtime and always has a different name.
you're namespaces shouldn't be randomly generated. if you're using a VS2005 website the binaries are randomly generated, but not the namespaces. other than that i don't see how this is possible.

if this is the case you can migrate to a "website application" project where a single binary is compiled instead of all the random garbage produced by a "website" project. moving forward I would stick with WAPs rather than website projects. they are easier to maintain and deploy.

a namespace cannot contain members/fields only classes (and structures). classes contain methods/functions, properties and fields.

you shouldn't need to instantiate an object with reflection. there are OOP techniques to abstract the implementation details from the intent using common OO patterns.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Hi

Are you maybe using some kind ob obfuscation software, that is causing you namespaces to change - this the only way I know that that could happen.
Also I am not shure that the line Assembly a = Assembly.Load("namespace2"); will work. In the argument you have to specify the name of the assembly and not the namespace contained in the assembly.

Uroš
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top