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
  1. Home
  2. General Programming
  3. C#
  4. Can an Attribute class find out, to which class it is actually attached?

Can an Attribute class find out, to which class it is actually attached?

Scheduled Pinned Locked Moved C#
csstutorialquestion
5 Posts 3 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.
  • A Offline
    A Offline
    Andy411
    wrote on last edited by
    #1

    Background: To implement localization I have derived from DisplayNameAttribute: LocalDisplayNameAttribute. When using this attribute, I only want to declare the ResourceString an a default string. The class LocalDisplayNameAttribute should find out on it's own, which class and which assembly is using it. My idea is, to find the strings in related resources by reflection. The strings should allways be in AssemblyName.Properties.Resources. Is it possible for a attribute class, to find out, who it is used by? Example: Assembly LocalizationTools.dll

    namespace LocalizationTools
    {
    public class LocalDisplayNameAttribute : DisplayNameAttribute
    {
    public LocalDisplayNameAttribute(string resourceName, string defaultText)
    : base(defaultText)
    {
    m_resourceName = resourceName;
    }

        public override string DisplayName
        {
            get
            {
                string name = string.Empty;
                try
                {
                    name = SomeVodoo(base.DisplayNameValue);
                }
                catch (Exception)
                {
                    name = base.DisplayNameValue;
                }
                return name;
            }
        }
        
        private string SomeVodoo(string defaultText)
        {
            // Find out to which class an assembly we belong. Then Find the Properties.Resources of the assembly
            // and find the m\_resourceName
        }    
    }
    

    }

    Assembly SomeClasses.dll

    public class Foo
    {
    [LocalizationTools.LocalDisplayName("DisplayName_ImportantProperty", "Important property XYZ")]
    public int ImportantProperty { get; set; }
    }

    And in SomeClasses.Properties.Resources you can find DisplayName_ImportantProperty PS: I have a IMO less elegant solution by declaring the type.

    [LocalizationTools.LocalDisplayName(typeof(SomeClasses.Properties.Resources), "DisplayName_ImportantProperty", "Important property XYZ")]
    public int ImportantProperty { get; set; }
    }

    Thanks in advance Andy

    D B 2 Replies Last reply
    0
    • A Andy411

      Background: To implement localization I have derived from DisplayNameAttribute: LocalDisplayNameAttribute. When using this attribute, I only want to declare the ResourceString an a default string. The class LocalDisplayNameAttribute should find out on it's own, which class and which assembly is using it. My idea is, to find the strings in related resources by reflection. The strings should allways be in AssemblyName.Properties.Resources. Is it possible for a attribute class, to find out, who it is used by? Example: Assembly LocalizationTools.dll

      namespace LocalizationTools
      {
      public class LocalDisplayNameAttribute : DisplayNameAttribute
      {
      public LocalDisplayNameAttribute(string resourceName, string defaultText)
      : base(defaultText)
      {
      m_resourceName = resourceName;
      }

          public override string DisplayName
          {
              get
              {
                  string name = string.Empty;
                  try
                  {
                      name = SomeVodoo(base.DisplayNameValue);
                  }
                  catch (Exception)
                  {
                      name = base.DisplayNameValue;
                  }
                  return name;
              }
          }
          
          private string SomeVodoo(string defaultText)
          {
              // Find out to which class an assembly we belong. Then Find the Properties.Resources of the assembly
              // and find the m\_resourceName
          }    
      }
      

      }

      Assembly SomeClasses.dll

      public class Foo
      {
      [LocalizationTools.LocalDisplayName("DisplayName_ImportantProperty", "Important property XYZ")]
      public int ImportantProperty { get; set; }
      }

      And in SomeClasses.Properties.Resources you can find DisplayName_ImportantProperty PS: I have a IMO less elegant solution by declaring the type.

      [LocalizationTools.LocalDisplayName(typeof(SomeClasses.Properties.Resources), "DisplayName_ImportantProperty", "Important property XYZ")]
      public int ImportantProperty { get; set; }
      }

      Thanks in advance Andy

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      You don't. An atribute should NOT EVER modify its behavior based on the class name it's attached to. The reason is that now your attribute functionality is directly tied to the classes it's looking for. That's bad! If you need to modify the behavior of the attribute, then you need two or more attributes, one for each behavior.

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak

      A 1 Reply Last reply
      0
      • D Dave Kreskowiak

        You don't. An atribute should NOT EVER modify its behavior based on the class name it's attached to. The reason is that now your attribute functionality is directly tied to the classes it's looking for. That's bad! If you need to modify the behavior of the attribute, then you need two or more attributes, one for each behavior.

        A guide to posting questions on CodeProject[^]
        Dave Kreskowiak

        A Offline
        A Offline
        Andy411
        wrote on last edited by
        #3

        Hi Dave, thanks for your answer. Well, I did not want to change the behaviour of the attribute. It still should show the DisplayName, Description, Category etc. I only wanted to make it a bit more "smart" regarding localization. I was just curious, if it is possibly for an attribute to figure out to which class it is attached. It's ok for me, since I have a solution, inspired by different approaches which can be found here at CP and out in the web.

        1 Reply Last reply
        0
        • A Andy411

          Background: To implement localization I have derived from DisplayNameAttribute: LocalDisplayNameAttribute. When using this attribute, I only want to declare the ResourceString an a default string. The class LocalDisplayNameAttribute should find out on it's own, which class and which assembly is using it. My idea is, to find the strings in related resources by reflection. The strings should allways be in AssemblyName.Properties.Resources. Is it possible for a attribute class, to find out, who it is used by? Example: Assembly LocalizationTools.dll

          namespace LocalizationTools
          {
          public class LocalDisplayNameAttribute : DisplayNameAttribute
          {
          public LocalDisplayNameAttribute(string resourceName, string defaultText)
          : base(defaultText)
          {
          m_resourceName = resourceName;
          }

              public override string DisplayName
              {
                  get
                  {
                      string name = string.Empty;
                      try
                      {
                          name = SomeVodoo(base.DisplayNameValue);
                      }
                      catch (Exception)
                      {
                          name = base.DisplayNameValue;
                      }
                      return name;
                  }
              }
              
              private string SomeVodoo(string defaultText)
              {
                  // Find out to which class an assembly we belong. Then Find the Properties.Resources of the assembly
                  // and find the m\_resourceName
              }    
          }
          

          }

          Assembly SomeClasses.dll

          public class Foo
          {
          [LocalizationTools.LocalDisplayName("DisplayName_ImportantProperty", "Important property XYZ")]
          public int ImportantProperty { get; set; }
          }

          And in SomeClasses.Properties.Resources you can find DisplayName_ImportantProperty PS: I have a IMO less elegant solution by declaring the type.

          [LocalizationTools.LocalDisplayName(typeof(SomeClasses.Properties.Resources), "DisplayName_ImportantProperty", "Important property XYZ")]
          public int ImportantProperty { get; set; }
          }

          Thanks in advance Andy

          B Offline
          B Offline
          BobJanova
          wrote on last edited by
          #4

          I'm fairly sure you can't do this without using a typeof(...) to pass it to the attribute's constructor.

          A 1 Reply Last reply
          0
          • B BobJanova

            I'm fairly sure you can't do this without using a typeof(...) to pass it to the attribute's constructor.

            A Offline
            A Offline
            Andy411
            wrote on last edited by
            #5

            Thx for your answer. Well, I have to accept my destiny ;-) and I'll choose the typeof operator in the constructor. As I mentioned before, I was just interested if it could be possible

            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