?
Solved

Type.GetType for unreferenced assembly

Posted on 2004-09-26
8
Medium Priority
?
1,366 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 252 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 248 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

Independent Software Vendors: 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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

800 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