0

Let's say we have two classes:

MainClass:

public class MainClass
{
    [CustomAttribute("John Smith", 55)]
    ItemClass ic = new ItemClass();
}

ItemClass:

public class ItemClass
{
    public void TestMethod()
    {
        // I want to get the "John Smith" and 55 from here
    }
}

And also a custom attribute CustomAttribute:

public class CustomAttribute : Attribute
{
    public string Name;
    public int Age;

    public CustomAttribute(string name, string age)
    {
        this.Name = name;
        this.Age = age;
    }
}

Is there any way I can get the that attribute (defined in MainClass) from inside an instance of my ItemClass?

In other words, how can I get "John Smith" and 55 from inside TestMethod()?

Jyclop
  • 469
  • 1
  • 6
  • 15
  • Looks very weird. Why you just not assign properties using a constructor or initilizers? – Serge Aug 06 '21 at 23:24
  • Does this answer your question? [Reflection - get attribute name and value on property](https://stackoverflow.com/questions/6637679/) and [How to get Attribute Value for property](https://stackoverflow.com/questions/44429382/) and [Custom attribute on property - Getting type and value](https://stackoverflow.com/questions/3289198/) and [Get attribute values from property and list values without knowing the attribute type](https://stackoverflow.com/questions/13076158/) and [get property value from object using custom attribute](https://stackoverflow.com/questions/50968126/) –  Aug 07 '21 at 00:59
  • @Serge Yes, I know this is a strange request, but I was more just wondering if it's possible. I'll probably end up doing it using a constructor. – Jyclop Aug 09 '21 at 07:14

1 Answers1

-1

Should work like this (untested):

public class ItemClass
{
    public void TestMethod()
    {
        // I want to get the "John Smith" and 55 from here
        CustomAttribute ca = typeof(MainClass)
            .GetField("ic", BindingFlags.Private | BindingFlags.Instance)
            .GetCustomAttributes(typeof(CustomAttribute), false)
            .FirstOrDefault() as CustomAttribute;

        Console.WriteLine("Name: {0}, Age: {1}", ca.Name, ca.Age);
    }
}
lidqy
  • 1,891
  • 1
  • 9
  • 11
  • GetField can return null: prefer `nameof(ic)` in C# 6+ otherwise do a null check. –  Aug 07 '21 at 01:04
  • 1
    @OlivierRogier `nameof(ic)` won't compile. It's `nameof(MainClass.ic)` Advising a null check at the same time is very bright, because GetField can not return null, if the nameof for that field compiles. So thx for the clever input. – lidqy Aug 07 '21 at 07:36
  • I guess I should've mentioned this in my original question, but would it be possible to do this completely generally? Basically, is there a way to do this that doesn't require `TestMethod` to know about `MainClass` or `"ic"`? – Jyclop Aug 09 '21 at 07:22
  • 1
    If the field whose attribute you want to read is in MainClass (or whatever other class) you have to know about that class and the field name. – lidqy Aug 09 '21 at 07:49
  • 1
    The only possibilty to not know the name of the field (or property?) and class would be to loop through all members of all types of all loaded assemblies and get their custom attributes. Which is doable, but kind of odd. – lidqy Aug 09 '21 at 09:36