Can an Attribute class find out, to which class it is actually attached?
-
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
-
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
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 -
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 KreskowiakHi 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.
-
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
-
I'm fairly sure you can't do this without using a typeof(...) to pass it to the attribute's constructor.