-
November 26th, 2009, 05:29 AM
#1
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.
I tried something like this:
[code]
Code:
public Type uClass;
...
Assembly *** = Assembly.GetExecutingAssembly();
foreach (Type type in ***.GetTypes())
{
if (type1.IsClass){
if (type1.Name == "myclass"){
uClass = type1;
}
}
}
But then, when I use uClass I get the error message:
" uClass is a 'field' but is used like a 'type' "
What goes wrong here? Or is there a much better way to do what I am trying to do? I mean, the next step is getting access to all the properties in this class. How would I do that?
Hope someone can get me back on track,
Thanks!
-
November 26th, 2009, 05:45 AM
#2
Re: reference to classes in another namespace
Obviously the part
Assembly *** = Assembly.GetExecutingAssembly();
should reference the other assembly!
-
November 26th, 2009, 07:05 AM
#3
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.
- Make it run.
- Make it right.
- Make it fast.
Don't hesitate to rate my post.
-
November 26th, 2009, 08:40 AM
#4
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
Code:
void UpdateSettings(uClass inst){
Console.WriteLine( inst.totalRate );
}
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?
Last edited by safra; November 26th, 2009 at 08:43 AM.
-
November 27th, 2009, 11:43 AM
#5
Re: reference to classes in another namespace
Is this possible at all?
All info I found so far about reflection is creating a new instance of a class using reflection.
if I do
Code:
Type uMyClass;
foreach (Type type1 in asm.GetTypes())
{
if (type1.IsClass){
if(type1.Name == "MyClass"){
uMyClass = typeof(type1);
}
}
}
I get the error:
The type or namespace name `type1' could not be found. Are you missing a using directive or an assembly reference?
Last edited by safra; November 27th, 2009 at 11:50 AM.
-
November 27th, 2009, 01:26 PM
#6
Re: reference to classes in another namespace
What problem are you trying to solve by dynamically creating namespaces at runtime?
-
November 27th, 2009, 02:39 PM
#7
Re: reference to classes in another namespace
That is not what I want to do but that is how the software works I want to use this class library in. My "public" c# scripts with the classes I need access to are part of that namespace.
Last edited by safra; November 27th, 2009 at 02:44 PM.
-
November 27th, 2009, 03:03 PM
#8
Re: reference to classes in another namespace
Originally Posted by safra
That is not what I want to do but that is how the software works I want to use this class library in. My "public" c# scripts with the classes I need access to are part of that namespace.
What problem are you trying to solve by dynamically creating namespaces at runtime?
-
November 28th, 2009, 04:21 AM
#9
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?
-
November 28th, 2009, 03:35 PM
#10
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?
-
November 28th, 2009, 05:16 PM
#11
Re: reference to classes in another namespace
Originally Posted by Arjay
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
-
November 29th, 2009, 05:19 AM
#12
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.
-
November 29th, 2009, 07:02 AM
#13
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:
1) Find class X which implements interface Y.
2) Instantiate it.
3) Use it.
Or is it something a lot more complex, or just something completely different?
www.monotorrent.com For all your .NET bittorrent needs
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.
-
November 29th, 2009, 06:05 PM
#14
Re: reference to classes in another namespace
-
November 30th, 2009, 05:15 PM
#15
Re: reference to classes in another namespace
dynamically generated class library by the software itself:
Namespace1
This Namespace includes the software runtime classes and Class1 which is a script added by me.
My class library:
Namespace2 with class2 with method
public void Method1(Namespace1.Class1 instance){
...
}
I want to use instances of a class in the dynamically generated library in my class library.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|