If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

reference to classes in another namespace

Hi,

I am new to c# so far so good. But I got stuck. I am creating a class library. Some of the methods in a class need a reference to an instance of a class in another namespace. I cannot simply add a reference to this namespace in the project because at runtime this namespace will be dynamically created. I do, however, know what the classes in this namespace look like.

Re: reference to classes in another namespace

If you need create an instance, use Activator.CreateInstance(uClass). But to call methods or access properties on the instance, you have to use reflection. To avoid it, make the classes in the assembly to derive from a common base class, or better implement an interface, and cast the result if Activator.CreateInstance() to that type.

Re: reference to classes in another namespace

Thanks Boudino, I am not really that much familiar with what you are saying. But I do not want to create an instance of the class in the other namespace. I want to reference instances of that class and get access to their properties and methods.

I belief I have to follow the reflector route. I did search and found out about reflector. Hence the code I posted but I didn't find any clear examples of the correct syntax for what I am trying to do neither did I find info telling more about the error message I am getting,

" uClass is a 'field' but is used like a 'type' "

The classes that I want to include in the class library are now part of the other namescape and all works fine but I want to seperate them. A very simple example of what I need to be able to do is this

uClass is no longer part of the same namespace so obviously it is not recogniozed. How do I get this to work when I cannot put a reference to this namespace in the project but I do have access to the corresponding assembly at runtime?

Re: reference to classes in another namespace

My classes in the dynamically created namespace have to be part of that namespace otherwise they cannot be used in this 3rd party software. Not sure though if that is what you meant by repeating your question?

Re: reference to classes in another namespace

Originally Posted by safra

My classes in the dynamically created namespace have to be part of that namespace otherwise they cannot be used in this 3rd party software. Not sure though if that is what you meant by repeating your question?

I repeated my question because it didn't look like I was getting a response.

Honestly it still isn't really clear why you need to dynamically create a namespace. If the namespace needs to match the 3rd party dll, don't you already know what the namespace needs to be?

Re: reference to classes in another namespace

Honestly it still isn't really clear why you need to dynamically create a namespace. If the namespace needs to match the 3rd party dll, don't you already know what the namespace needs to be?

I never have heard about a possibility to dynamically add a reference to a namespace.
because if you dont know the namespace a then you also would not know the classes names and properties, so what would you want to do with that classes? Its useless to set any unknown Property without knowing its purpose. So your whole request is a bit of unclear. What do you want to do with all that.? Whats the purpose of the whole application? And which problem are you trying to solve that way?

Jonny Poet

To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. ! Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]If anyone felt he has got help, show it in rating the post. Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?My latest articles :Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

Re: reference to classes in another namespace

The 2nd dynamically created namespace is created by the third party software and it includes runtime classes of this 3rd party software plus some of my classes that have to be in this dynamically created namespace.

Why are they creating the namespace dynamically? Because the software allows their users to add c# sharp scripts which is what I do. Each time I update a script in the third party software it is compiled and a new namespace is created.

Re: reference to classes in another namespace

What you're describing sounds like an incredibly insane system. It also makes next to no sense. Take a step back for a sec and forget about namespaces. What it sounds like you're trying to describe is a normal plugin like system where arbitrary third party libraries can be loaded by your program and executed. Is this the case? If so, how does the main application know what to load in the third party library? Is there a specific interface/class which the third party library must implement?

What is it you're trying to do exactly? So we go right back to this question:

What problem are you trying to solve by dynamically creating namespaces at runtime?

But i'd also add in: Why do you even care what namespace something is in? What's the high level task you're trying to accomplish? Is it:

NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.