Solved

Type.GetType for unreferenced assembly

Posted on 2004-09-26
8
1,332 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

809 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