C#-读取更新方法中的属性值
我有一个名为C#-读取更新方法中的属性值,c#,methods,attributes,C#,Methods,Attributes,我有一个名为 [Usermustwait(UserHaveToWait = 0, UserMustWait = false)] 在这个属性中,方法更新两个变量,我想得到这些值,而不是我定义的值。。。 我尝试了下面的示例,但他返回0和false GetType().GetMethod(currentMethodName).GetCustomAttributes(true).OfType<Usermustwait>().FirstOrDefault(); 我认为您希望使用反射来检查对
[Usermustwait(UserHaveToWait = 0, UserMustWait = false)]
在这个属性中,方法更新两个变量,我想得到这些值,而不是我定义的值。。。
我尝试了下面的示例,但他返回0和false
GetType().GetMethod(currentMethodName).GetCustomAttributes(true).OfType<Usermustwait>().FirstOrDefault();
我认为您希望使用反射来检查对象的实例,而不是类型定义如何在方法中使用反射?我的属性是在方法上定义的,而不是class@wdomains不能为对象实例设置属性。@maxime是否在方法上使用了此属性?您可以显示您的使用情况吗?当您通过
GetCustomAttribute
获取属性时,您无法在运行时更改属性中的值,此时将使用注释中的值创建一个新实例。如果您想跟踪变量状态,您可以使用类上的字段,也许您想通过属性设置默认值,但在这种情况下很难看到它们的用法。
public class UserMustWait : PreconditionAttribute
{
public bool UserMustWait { get; set; } = false;
public int UserHaveToWait { get; set; } = 0;
public UserMustWait()
{
}
public override Task<PreconditionResult> CheckPermissionsAsync(ICommandContext context, CommandInfo command, IServiceProvider services)
{
// doing some stuff to verify in a SQLDatabase if the user catched in the context have a specified role or not
// if have not a specified role :
// UserMustWait = true;
// UserHaveToWait = XX;
// I need to read these value in my method to be sure the user wait the number in milisecond
return Task.FromResult(PreconditionResult.FromSuccess());
}
}
[Usermustwait(UserHaveToWait = 0, UserMustWait = false)]
public async Task join(string arg)
{
// this is where i need to read the value updated by the attribut
// if(UserMustWait = true)
// SendMessage("You have to wait {UserHaveToWait} secondes");
//
}