all groups > dotnet windows forms designtime > october 2005 >
You're in the

dotnet windows forms designtime

group:

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


How to reflect on application assembly from a library assembly in designer Bill Henning
10/19/2005 8:49:29 PM
dotnet windows forms designtime:
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

Re: How to reflect on application assembly from a library assembly in designer Dira
10/20/2005 1:27:30 AM
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.
AddThis Social Bookmark Button