View Single Post
  #9 (permalink)  
Old March 24th, 2005, 01:44 PM
jacob jacob is offline
Friend of Wrox
Join Date: Jun 2003
Location: Copenhagen, , Denmark.
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts


quote:You said you "interfaced" yourself out of the problem. Are you able to do what I was trying to do above with that method? I need to learn more about interfaces I guess, but it does seem like there should be an easier way.
No, I am not yet able to do what you would like... and I would like. To get smarter I would still like to know the answer when it finally appears :)

What I did to solve my problem was to use an interface for all the possible classes it could be. Look at the following code which is an extract from a method I have...
// Getting an array of file information about the files matching the
// pattern given.
info = new DirectoryInfo(folder).GetFiles(filePattern);
foreach(FileInfo f in info)
    a = Assembly.LoadFrom(f.FullName);
    foreach(Type t in a.GetTypes())
        // Retrieving the attribute with type attributeType if 
        // existing...
        attribute = (IPluginAttribute)Attribute.GetCustomAttribute(t
            , attributeType);
        // If the attribute is found and the property Name equals
        // what we are looking for the assembly is returned.
        if(attribute != null && attribute.Name.Equals(name))
            this.type = t;
            this.assembly = a;
The above code is part of a custom-made plugin-handler, which loads assemblies and check if some specific attribute is set for some class. If it is I have the right class, which is the plugin module.

However in order to make this general (since it require a type cast) I had to make the different attributes implement an interface, which I then can use for the cast. In this case the interface is named IPluginAttribute and as it can be seen it has the property Name.

Hope it helps, Jacob.