CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 21
  1. #1
    Join Date
    Oct 2009
    Posts
    16

    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!

  2. #2
    Join Date
    Oct 2009
    Posts
    16

    Re: reference to classes in another namespace

    Obviously the part

    Assembly *** = Assembly.GetExecutingAssembly();

    should reference the other assembly!

  3. #3
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    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.

  4. #4
    Join Date
    Oct 2009
    Posts
    16

    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.

  5. #5
    Join Date
    Oct 2009
    Posts
    16

    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.

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: reference to classes in another namespace

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

  7. #7
    Join Date
    Oct 2009
    Posts
    16

    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.

  8. #8
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: reference to classes in another namespace

    Quote Originally Posted by safra View Post
    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?

  9. #9
    Join Date
    Oct 2009
    Posts
    16

    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?

  10. #10
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: reference to classes in another namespace

    Quote Originally Posted by safra View Post
    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?

  11. #11
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: reference to classes in another namespace

    Quote Originally Posted by Arjay View Post
    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

  12. #12
    Join Date
    Oct 2009
    Posts
    16

    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.

  13. #13
    Join Date
    May 2007
    Posts
    1,546

    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.

  14. #14
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: reference to classes in another namespace

    or just BAD (as in AUP?)
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  15. #15
    Join Date
    Oct 2009
    Posts
    16

    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.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured