Solved

Type.GetType for unreferenced assembly

Posted on 2004-09-26
8
1,314 Views
Last Modified: 2007-12-19
Hi

I understood when you had a project WITHOUT a reference to an assembly you could get the type but the following
Type type = Type.GetType("MyCompany.Config.Panes.Email, MyCompany.Config.Panes");
where
My.Conpany.Config.Panes.Email is the UserControl
MyCompany.Config.Panes = assembly where the control is

and it is being called from MyCompany.Config - which is another assembly

The MyCompany.Config.Panes is not in the gac - but I have also tried the string as
"MyCompany.Config.Panes.Email, MyCompany.Config.Panes, Version=1.0.1731.20479, Culture=neutral, PublicKeyToken=null"
but this doesn't work either
nor does "MyCompany.Config.Panes.Email" on it's own.

Many thanks
0
Comment
Question by:esbesb
  • 3
8 Comments
 
LVL 10

Expert Comment

by:eternal_21
ID: 12156481
You cannot get the type in the manner that you have described.  Do you know the location of the assembly on the computer?  If so, you could dymanically load this assembly (MyCompany.Config.Panes), and then get types from it.
0
 

Author Comment

by:esbesb
ID: 12156630
I understand to dynically load it you would use
                string _assemblyFile = @"D:\Projects\AM\MyCompany.Config.Panes.dll";                      
                ObjectHandle objectHandle = Activator.CreateInstanceFrom(assemblyFile, typeName);
                object phantomObject = objectHandle.Unwrap();

but how can you get the type for the objectHandle if you can't reference it?
        _assembly = Assembly.LoadFrom(MyCompany.Config.Panes);
fails when not referenced so can't do
     Type[] _mytypes = _assembly.GetTypes();                                        
    foreach(Type t in _mytypes)
    {
         if (t.FullName == typeParts[0].ToString())
        {
               type = t.GetType();
        }                              
    }

My manager swears you can - in fact he is irrate with me for not pulling it off! Help.

0
 
LVL 19

Accepted Solution

by:
drichards earned 63 total points
ID: 12157273
>> but how can you get the type for the objectHandle if you can't reference it?
I'm not sure exactly what you're asking, but maybe this will help.  The code you show to find types in an assembly should work.  However, you'll be stuck with the generic 'Object' when you're done unless you can reference the assembly with the type in it (in which case you wouldn't have had to load it in the first place).  If it is some kind of plugin thing, you usually would define a base class in an assembly you can reference.  Then when you dynamically load the plugin assemblies you instantiate the types and cast them to the base type which is available through the reference.

If you just load an arbitrary assembly, you can find and instantuate the types, but you are stuck having references to generic objects and using reflection to call methods.
0
 
LVL 10

Assisted Solution

by:eternal_21
eternal_21 earned 62 total points
ID: 12161740
 string assemblyFile = @"MyCompany.Config.Panes.dll";
  System.Reflection.Assembly assembly = System.Reflection.Assembly.LoadFrom(assemblyFile);
 
  // Now you can enumerate the Types in 'assembly':

  foreach(System.Type type in assembly.GetTypes())
  {
    System.Diagnostics.Debug.WriteLine(type.AssemblyQualifiedName);
  }

  // Or get a specifc Type:

  System.Type type = assembly.GetType("MyCompany.Config.Panes.Email", true);
  System.Diagnostics.Debug.WriteLine(type.AssemblyQualifiedName);
0
 
LVL 10

Expert Comment

by:eternal_21
ID: 12161794
Once you call System.Reflection.Assembly.LoadFrom, the assembly is loaded into the current Application Domain, and types would be available from System.Type.GetType, providing you use fully qualified names.  But as drichards said, at compile time all of your use in MyCompany.Config of the MyCompany.Config.Panes.dll is through objects (child properties accessed using reflection) or some common base type (child properties accessed through inheritence or an interface).
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now