Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Compare types in a plugin API

Compare types in a plugin API

Scheduled Pinned Locked Moved C#
designjsonhelp
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jost Pedro
    wrote on last edited by
    #1

    Hi there, I’m trying to implement a plug in API in a application and I’m having problems in getting the Custom Attribute from an Assembly. First the custom attribute is something like this: [global::System.AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] public class AssemblyPluginAttribute : Attribute { readonly string pluginName; // This is a positional argument. public AssemblyPluginAttribute(string PluginName) { this.pluginName = PluginName; } public string PluginName { get { return this.pluginName; } } } Than in order to find a reference to this custom attribute I added to the plug-in project the following line. ... using Enki.UI.ComponentModel; [assembly: AssemblyPluginAttribute("TestPlugin")] namespace TestPlugin { ... Now ... to find if its a plug-in type assembly i look through the custom attributes of the assembly in order to find one matching with the one i defined (AssemblyPluginAttribute). The following line returns null list. Type AttType = typeof(Enki.UI.Plugins.AssemblyPluginAttribute); object[] obj = assembly.GetCustomAttributes(AttType, true); and the next one too... Attribute[] attlist= Attribute.GetCustomAttributes(assembly,AttType); but if instead of one of this two lines i use the following code ... foreach (Attribute att in Attribute.GetCustomAttributes(assembly)) { if (att.GetType().FullName.CompareTo(AttType.FullName)==0) return true; } ... it matches the full names!! I’ve tried many other ways to establish a possible comparison but none has the same result. I know, that if i found a way to get the result i desired i should stick to it an go on but I believe that this might lead me to a dead end in the future. Please, can anyone help me... to "see the light in the end of the tunnel"! :) Tanks Pedro M.

    L 1 Reply Last reply
    0
    • J Jost Pedro

      Hi there, I’m trying to implement a plug in API in a application and I’m having problems in getting the Custom Attribute from an Assembly. First the custom attribute is something like this: [global::System.AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] public class AssemblyPluginAttribute : Attribute { readonly string pluginName; // This is a positional argument. public AssemblyPluginAttribute(string PluginName) { this.pluginName = PluginName; } public string PluginName { get { return this.pluginName; } } } Than in order to find a reference to this custom attribute I added to the plug-in project the following line. ... using Enki.UI.ComponentModel; [assembly: AssemblyPluginAttribute("TestPlugin")] namespace TestPlugin { ... Now ... to find if its a plug-in type assembly i look through the custom attributes of the assembly in order to find one matching with the one i defined (AssemblyPluginAttribute). The following line returns null list. Type AttType = typeof(Enki.UI.Plugins.AssemblyPluginAttribute); object[] obj = assembly.GetCustomAttributes(AttType, true); and the next one too... Attribute[] attlist= Attribute.GetCustomAttributes(assembly,AttType); but if instead of one of this two lines i use the following code ... foreach (Attribute att in Attribute.GetCustomAttributes(assembly)) { if (att.GetType().FullName.CompareTo(AttType.FullName)==0) return true; } ... it matches the full names!! I’ve tried many other ways to establish a possible comparison but none has the same result. I know, that if i found a way to get the result i desired i should stick to it an go on but I believe that this might lead me to a dead end in the future. Please, can anyone help me... to "see the light in the end of the tunnel"! :) Tanks Pedro M.

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #2

      Here is the factory method I use in a plug-in application I have. You do not appear to have a need for the "ICustomAttributeHandler" aspect of the logic so you should ignore that part.

      public static object newInstanceFromCustomAttribute( Assembly assm, System.Type tAttribute, ICustomAttributeHandler handler)
      {
          object oret = null;
          Module[] modules = assm.GetModules(false);        // no resource modules needed
          for(int nMod=0; null == oret && nMod<modules.Length; nMod++)
          {
              Type[] types = modules[nMod].GetTypes();
              for(int nType=0; null == oret && nType<types.Length; nType++)
              {
                  // get the array of attributes of our target type
                  object[] attributes = types[nType].GetCustomAttributes(tAttribute, true);   
                  for(int nAttr=0; null == oret && nAttr<attributes.Length; nAttr++)
                  {
                      // ask the handler if that is what it is looking for
                      if( handler.attributeQualifies( attributes[nAttr] as Attribute))
                          oret = assm.CreateInstance( types[nType].FullName);
                  }
              }    // for each type
          }    // for each module
          return oret;
      }

      led mike

      J 1 Reply Last reply
      0
      • L led mike

        Here is the factory method I use in a plug-in application I have. You do not appear to have a need for the "ICustomAttributeHandler" aspect of the logic so you should ignore that part.

        public static object newInstanceFromCustomAttribute( Assembly assm, System.Type tAttribute, ICustomAttributeHandler handler)
        {
            object oret = null;
            Module[] modules = assm.GetModules(false);        // no resource modules needed
            for(int nMod=0; null == oret && nMod<modules.Length; nMod++)
            {
                Type[] types = modules[nMod].GetTypes();
                for(int nType=0; null == oret && nType<types.Length; nType++)
                {
                    // get the array of attributes of our target type
                    object[] attributes = types[nType].GetCustomAttributes(tAttribute, true);   
                    for(int nAttr=0; null == oret && nAttr<attributes.Length; nAttr++)
                    {
                        // ask the handler if that is what it is looking for
                        if( handler.attributeQualifies( attributes[nAttr] as Attribute))
                            oret = assm.CreateInstance( types[nType].FullName);
                    }
                }    // for each type
            }    // for each module
            return oret;
        }

        led mike

        J Offline
        J Offline
        Jost Pedro
        wrote on last edited by
        #3

        Hi mike, i believe that i miss guided you. I dont want to find which types in an assembly have a custom attribute. I pretend to find which assembly has a specific custom Attribute. So, want happens is I have an assembly that i know for sure that the custom attribute and i the only way that I get it saying that it has that attribute (return true) is comparing their FullName which,i think, is a rude solution to the problem. Tanks for your help. Best Regards Pedro M.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups