Solved

Type.GetType for unreferenced assembly

Posted on 2004-09-26
8
1,349 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
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…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…

707 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