Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / Design Time / October 2005

Tip: Looking for answers? Try searching our database.

How to reflect on application assembly from a library assembly in designer

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Bill Henning - 20 Oct 2005 01:49 GMT
Ok, this is a really wacky situation but I have this scenario:
1) An application project.
2) A library project with controls used by the application.

In the designer of one of the controls, what I want to do is enumerate
through all the classes in the application project and look for classes that
inherit from a class in the library project so that I can build a list of
them and display them to the user.

This code snippet works fine if I run it at run-time but not at design-time.
It seems like the application's assembly is not loaded in the AppDomain when
designing one of the Forms in the application:

--
Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
foreach (Assembly assembly in assemblies) {
   Type[] types = assembly.GetTypes();
   foreach (Type type in types) {
       if (type.IsSubclassOf(targetType))
           MessageBox.Show(type.FullName);
   }
}
--

None of the Assembly.GetXXX methods return the application's assembly
either.  I did find that Assembly.Load will load it if I pass the name,
however I don't know the name unless I hardcode it and since other apps will
use the library assembly, I can't hardcode the name.

So my question is, while designing a Form in the application project, from a
Designer class in the referenced library project, how can I get the Types in
the application project so that I can enumerate through them?

Or, even if someone can tell me how to get the full name of the application
assembly from the library, that would enable me to use Assembly.Load.

Thanks in advance,
Bill
Dira - 20 Oct 2005 09:27 GMT
Hallo Bill,

You are right, it is not [always] possible to inspect the resulting
assembly in design time. And you will see the reason if you consider,
that you sources must not be compiled or even compilable at the moment
you explore the assembly.
The Assembly must not exist at all.

There is a different mechanism to explore classes in a project that
is not compiled/compilable.

First get DTE with
  EnvDTE._DTE dte = (EnvDTE._DTE)context.GetService( typeof(
EnvDTE._DTE));

Then enumerate all projects in the solution and get CodeModel to each
project:
  foreach( EnvDTE.Project prj in dte.Solution.Projects){
     VSLangProj.VSProject vsprj = (VSLangProj.VSProject)prj.Object;
     EnvDTE.CodeModel codeModel = prj.CodeModel;
     if( codeModel != null){

Enumerate items of codeModel.CodeElements:
  foreach( EnvDTE.CodeElement celm in codeElements){

CodeElements we must consider are of sub-type EnvDTE.CodeNamespace and
EnvDTE.CodeClass. They build a tree structure and must be enumerated
recursively (both contain their children in Members property).
Each CodeClass must be tesed:
     if( ccl.get_IsDerivedFrom( fullName))
         // add to the collection
         //   and dont forget to walk down the tree: a class can have
embedded
sub-classes.

It works for me. I have even written an abstract class to inherit from.

HTH,
Dima.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.