0

Here's roughly what I had in mind:

Class which should get the attribute:

 // This should be dynamically - if the if block in the ctor of 
 // AuthorizeAttribute is true here should be 
 // [MyOwn1] if its false [MyOwn2]
[MyOwn]
public class MyClass
{
    // Check
}

Class which should give the attribute:

public class MyOwnAttribute : Attribute
{

    public AuthorizeAttribute()
    {
        if (SomeMethodOfAnotherClassCheck())
        {
            [MyOwn1];
        {
        else
        } 
            [MyOwn2];
        }
    }

}

Now when MyClass asks for the attribute from the MyOwnAttribute class, it should get the correct one depending on the if condition included in the ctor.

I have no idea how to implement this. Can someone help me here please?

  • Where does condition come from? – rene Dec 23 '20 at 13:19
  • 2
    This sounds like an XY problem to me, what exactly are you trying to achieve? – DavidG Dec 23 '20 at 13:20
  • The condition is just a method of an other class. No inputs or parameters are needed for it. – 10minQuestion Dec 23 '20 at 13:25
  • is AuthorizeAttribute() a Ctor of MyOwnAttribute? what is [MyOwn1] and [MyOwn2]? – Karim Moghnieh Dec 23 '20 at 13:25
  • @DavidG I like to have the attribute of "MyClass" dynamically. Depending on the if block in the ctor. For sure can put this if block also somewhere else if needed. Its just an idea how it could work – 10minQuestion Dec 23 '20 at 13:28
  • You can't choose attributes like this, that is why I'm asking what you are actually trying to do. – DavidG Dec 23 '20 at 13:28
  • You can't at runtime switch the Attribute type that gets instantiated. You can have the implementation of MyOwn attribute behave differently based on a condition but you can't replace it. – rene Dec 23 '20 at 13:35
  • u can't change the type like this, instead, u can add a property in the MyOwnAttribute (named type for example) and fill it in the if statement – Karim Moghnieh Dec 23 '20 at 13:45
  • @rene I like to create more less my own "AuthorizeAttribute" of MVC. I like to give only access to the controller if the user with the mail adress "abc.ab@domain.com" is logged in. If this user is logged in => allow to use controller if not this user => don`t allow user – 10minQuestion Dec 23 '20 at 13:48
  • In .Net core? see https://stackoverflow.com/questions/17272422/multiple-authorization-attributes-on-method?noredirect=1&lq=1 or https://stackoverflow.com/questions/2071235/overriding-controller-authorizeattribute-for-just-one-action?noredirect=1&lq=1 – rene Dec 23 '20 at 14:11

0 Answers0