How could I create a method that does not need to receive a type as a parameter, only a string
, but would return a type?
As for example:
public static T Guard(string guard_name) where T : class, IAuth
{
return (T)Activator.CreateInstance(Type.GetType(guard_name));
}
Instead of:
public static T Guard<T>(string guard_name) where T : class, IAuth
{
return (T)Activator.CreateInstance(Type.GetType(guard_name));
}
Then the call would look like this: Auth.Guard("users")
instead of Auth.Guard<users>()
.
I need a method that only receives a string and not a type because I'm working with a class ActionFilterAttribute
that the statement is [Autorized(string guard_name)]
and therefore I can not use a type at that time. I've already tried variations like [Autorized<users>]
for example, but because it was an attribute I could not do it.
Is it possible?