2

I have a method in which I am invoking methods of different classes at run time using reflection, code is below:

var assembly = System.Reflection.Assembly.GetExecutingAssembly();
string[] assemblyInfo = assembly.FullName.Split(',');
var controllerType = assembly.GetType(assemblyInfo[0] + ".Class1");
var controllerObj = System.Activator.CreateInstance(controllerType);
var method = controllerObj.GetType().GetMethod("Method1");
object[] parameters = new object[] { "-SomeParameterObject-" };
var obj = method.Invoke(controllerObj, parameters);

And each invoked method has a Custom Attribute but it's not getting invoked in reflection. E.g.

[ReadWrite]
public Object SomeMethodName(Object param)

Is there any way in reflection by which custom attribute should get invoked automatically with method invocation?

dymanoid
  • 14,771
  • 4
  • 36
  • 64
BhushanK
  • 1,205
  • 6
  • 23
  • 39
  • 2
    Can you explain what "custom attribute invocation" means? – IS4 Sep 12 '17 at 10:12
  • @lllidanS4 it's mean when we invoke `SomeMethodName` method using reflection then `[ReadWrite]`(which is a custom attribute) should also get invoked. – BhushanK Sep 12 '17 at 10:38
  • Thats not how Attributes work. The code that invokes the method needs to check for the attribute and must do the appropiate. What is appropiate is attribute dependent and often not contained in the attribute itself but somewhere else. – Ralf Sep 12 '17 at 10:42
  • You still didn't explain what it means to invoke an attribute. Do you have a clear idea of it? One does not "invoke" an attribute. Attributes are custom pieces of metadata, they are not pieces of code. If you want to obtain the attribute, call *GetCustomAttribute*. It will "invoke" the constructor, but its only purpose in the attribute is to store the data passed to it for later retrieval. – IS4 Sep 12 '17 at 10:57

0 Answers0