Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Assembly.LoadFrom(string) - help!

Status
Not open for further replies.

Oxymoron

Technical User
Dec 17, 2000
168
GB
I am using the code below to load an assembly from a .dll file. Each of the classes contained within are inspected to see if they implement my “IControl” interface.

If they do, I began reflecting them to look at their properties and especially their custom Attributes.

To look at their custom attributes I have to create a Type object passing the AssemblyQualifiedName property as a parameter to the GetType(string) method, because for some reason if i just called GetCustomAttributes() on the typesInAssembly[k] Type object, the if construct inside the foreach...loop would not evaluate to true.



Code:
Type[] typesInAssembly = null;

try
{

   Assembly myAssembly = Assembly.LoadFrom(myPluginDLLfiles[i].FullName);

   typesInAssembly = myAssembly.GetTypes();

}
catch (Exception e)
{

   //do something with exception

}

Type tempControl = null;

for (int k=0;k < typesInAssembly.Length;k++)
{

     /*See if any of the Types in the assembly implement the IControl interface*/

      Type anInterface = typesInAssembly[k].GetInterface("IControl");

 

   if ((anInterface != null) && (typesInAssembly[k].IsClass))
   {

      ControlDescriptor currentControlD = new ControlDescriptor(filesInDir[i]); //create new ControlDescriptor

      /*Needs to be a fully qualified assembly name*/

      tempControl = Type.GetType(typesInAssembly[k].AssemblyQualifiedName);  //ASSIGNMENT FAILS!!!!

      ControlAttribute controlAttr = null;
      object[] attributes = tempControl.GetCustomAttributes(false);

      foreach (Attribute attr in attributes)
      {                          
         controlAttr = attr as ControlAttribute;
         if (controlAttr != null)
         {
            /*set ControlDescriptor properties to those reflected from control*/
            currentControlD.Description = controlAttr.ControlDescription;
         }
      }

   }

}



My problem is; The above code works fine on my machine. However, on my colleagues' machines it does not work. I find that the AssemblyQualifiedName property passed in to the GetType(string) method has different version numbers, but don't know why!

I'm afraid I am a newbie at C# and Assemblies, reflection etc.

If anyone could give me any idea why the above code works for me, but not for my colleagues I would be very grateful.
All suggestions appreciated!
Many thanks

Oxy




we are all of us living in the gutter.
But some of us are looking at the stars.
 
Reflection means the access to metadata.
The access is controlled by the ReflectionPermission class and I think your code should use it to demand the permission to discover and invoke code from other assemblies.
Check on the coleagus machines in Control Panel->Administrative Tool->Microsoft .NET Framework 1.1 Configuration->Run time Security Policy.
Expand the tree and inspect User & Machine ->Permission Sets->Everything ->Reflection permissions.
Check Internet set if your app uses Internet code.
-obislavu-

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top