locked
System.Reflection Assembly management / recursing RRS feed

  • Question

  • I would like to query namespaces within an assembly to find a class with a specific name and inheritance.  I am sure this knowledge exists somewhere, I just can't seem to find it (the keywords point to too many possibilities).  There is just a small amount more information that I need then I can find... if you take my meaning.  Not all of the example which follows is accurate, but it is intended to get the message across.

    object GetObjectReference( string objectName, System.Type type, [Unknown Type] SCOPE)

    {

        foreach( [Unknown Type] _NameSpace in SCOPE )

        {

              object o = GetObjectReference( objectName, type, _NameSpace, Asm );

              if( o != null )
                     return( o );

        }
        foreach( [Unknown Type] _Class in SCOPE )
        {
              /// OVER SIMPLIFIED
              if( _Class.GetName().ToLower() == objectName.ToLower() )
              {
                     object o = _Class.GetConstructor().Construct();
                     if( typeOf( o ) == type )
                             return( o );

             }
        }

    }

    Wednesday, October 11, 2006 5:49 PM

Answers

  • I found it... but just in case someone wonders I have outlined the conclusion below.  If someone has a better wy, please feel free to post.

    System.Reflection.Assembly Asm;

    // ... Code to acquire the assembly... sometimes:

    Asm = this.GetType().Assembly

    System.Type[] types = Asm.GetTypes();

    foreach( System.Type t in types )

    {

           /// Query the type and find the result.

          /// no recursing neccessary.

    }

    Wednesday, October 11, 2006 6:15 PM

All replies

  • I found it... but just in case someone wonders I have outlined the conclusion below.  If someone has a better wy, please feel free to post.

    System.Reflection.Assembly Asm;

    // ... Code to acquire the assembly... sometimes:

    Asm = this.GetType().Assembly

    System.Type[] types = Asm.GetTypes();

    foreach( System.Type t in types )

    {

           /// Query the type and find the result.

          /// no recursing neccessary.

    }

    Wednesday, October 11, 2006 6:15 PM
  • I just show you a example on how to access  Dot  Net Math Library

    // get a reflected assembly of the System assembly
                Assembly systemAssembly = Assembly.GetAssembly(typeof(System.Math));
                try
                {
                    //cant call the entry method if the assembly is null
                    if (systemAssembly != null)
                    {
                        //Use reflection to get a reference to the Math class

                        Module[] modules = systemAssembly.GetModules(false);
                        Type[] types = modules[0].GetTypes();

                        //loop through each class that was defined and look for the first occurrance of the Math class
                        foreach (Type type in types)
                        {

                        }

      If som guy have reference stuff on this subject, pls post it ,Thank you.

    Thursday, October 12, 2006 8:22 AM
  • I appreciate your contribution Jackply, and it raises a question that I wonder if you can answer.  What is the symbolic difference between the objects returned by GetModules and the object returned by GetTypes?  Does GetModules ONLY return classes?  This is what I wrote yesterday and it seems to work quite well!

    System.Type[] types = Asm.GetTypes();

    foreach (System.Type t in types)
    {
        if (t.Name.ToLower().Trim() == DriverName.ToLower().Trim() || checkMemeberForReferenceAttribute(DriverName, t))
        {
          
    if (t.IsSubclassOf(System.Reflection.Assembly.GetExecutingAssembly().GetType("lib._base.DataSource", false, true)))
           {
               
    try
               
    {
                   
    return ((_base.DataSource) t.GetConstructor(new System.Type[0]).Invoke(new object[0]));
               
    }
               
    catch (System.Exception e) {return (null);}
            }
        }
    }

    Friday, October 13, 2006 3:38 PM