I'm trying to set an attribute on a method when I create the object but not sure how to go about it (or if it's possible).
This is what I've tried so far:
public abstract class AbstractClass
{
    public abstract string DynamicAttributeValue { get; }
    public AbstractClass()
    {
        // Set the Attribute on THIS instance
        TypeDescriptor.AddAttributes(typeof(AbstractClass).GetMethod("MyMethod"),
            new Attribute[] {
                new ConditionalAttribute(DynamicAttributeValue)
        });
    }
    public void MyMethod()
    {
    }
}
class MyConcreteClass : AbstractClass
{
    public override string DynamicAttributeValue => "MyRunTimeValue";
    static void Main(string[] args)
    {
        MyConcreteClass myClass = new MyConcreteClass();
        string callingMethodName = "MyMethod";
        MethodInfo methodInfo = myClass.GetType().GetMethod(callingMethodName);
        var attributes = methodInfo.GetCustomAttributes();
    }
}
Aucun commentaire:
Enregistrer un commentaire