0

I have bunch of classes and a custom attribute class. All my classes have unique id which defined in my attribute. I add instance of my classes in a list randomly. How can I identify which class by just checking attribute.

Basicly,

I have an attribute class

public class myAtt : Attribute
{
   public int id;

   public myAtt(int id){ this.id = id; }
}

I have an enum

public enum myEnum
{
   IT_IS_A_CLASS,
   IT_IS_B_CLASS,
   IT_IS_C_CLASS
}

Bunch of classes that have this attribute

[myAtt((int)myEnum.IT_IS_A_CLASS)]
public class A
{
   
}

[myAtt((int)myEnum.IT_IS_B_CLASS)]
public class B
{
   
}

[myAtt((int)myEnum.IT_IS_C_CLASS)]
public class C
{
   
}

I have a thread function that dequeue item from queue that filled by another thread. Do not mind thread-safety.

while(true)
{
   var temp = myQ.Dequeue();
   switch((myEnum)temp.id)   <-- I basicly want to this entire switch become one-liner.
   {
      case myEnum.IT_IS_A_CLASS:  aFunction(new A()); break;
      case myEnum.IT_IS_B_CLASS:  aFunction(new B()); break;
      case myEnum.IT_IS_C_CLASS:  aFunction(new C()); break;
      .
      .
      .
      case myEnum.IT_IS_Z_CLASS:  aFunction(new Z()); break;
   }
}

I basicly want to remove this entire switch and become one-liner. Like

while(true)
{
   var temp = myQ.Dequeue();
   aFunction(magicFunction((myEnum)temp.id));
}
  • Does this answer your question? [How do I read an attribute on a class at runtime?](https://stackoverflow.com/questions/2656189/how-do-i-read-an-attribute-on-a-class-at-runtime) – GSerg Nov 07 '20 at 06:54
  • 2
    However please note that it [feels like](https://meta.stackexchange.com/q/66377/147640) you are trying to invent polymorphism from scratch, and you should really use inheritance and virtual methods. – GSerg Nov 07 '20 at 06:55
  • And what does `aFunction` look like? 26 overloads or are these classes in an inheritance tree? – Caius Jard Nov 07 '20 at 06:58
  • why would you not use inheritance? then you would know which class is which. you will be able to view all child classes this way and in turn, do your 1 liner – Getwrong Nov 07 '20 at 07:12
  • 2
    I understand the mechanics of what you are trying to do, I do not understand why you are trying to do this. To know the type of object simply use the "is" on class or interface. You can use this.GetType() or type() the property names do not have to have matching property types. Tell us why you like to do, perhaps the answers will be more useful – Walter Verhoeven Nov 07 '20 at 07:56

1 Answers1

0

To answer your question the following will work. However, as the comments on your question suggest, this really goes against the whole idea of polymorphism.

There are a lot of questions that go along with a situation like this as well, such as what happens when more than one class share the same attribute?

Also, if you really need to dynamically activate classes at runtime, you could simply use a Queue<Type> instead of using a custom emum.

public static object magicFunction(myEnum value)
{
    return Activator.CreateInstance(
        Assembly.GetExecutingAssembly()
            .DefinedTypes
            .FirstOrDefault(x => x.GetCustomAttribute<myAtt>()?.id == (int)value));
}
Jerry
  • 1,477
  • 7
  • 14