Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1421
  • Last Modified:

Type.GetType for unreferenced assembly

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
esbesb
Asked:
esbesb
  • 3
2 Solutions
 
eternal_21Commented:
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
 
esbesbAuthor Commented:
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
 
drichardsCommented:
>> 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
 
eternal_21Commented:
 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
 
eternal_21Commented:
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now