小编Sam*_*del的帖子

传递带有变量值的自定义属性作为参数

我创建了一个自定义属性类,它将检查系统安全性,并在出现安全性错误时抛出身份验证异常.

public class EntityChecker: System.Attribute
{
    public EntityChecker(int entityId)
    {
        // doing some logic to check if the entityId is allowed to be inserted
    }
}
Run Code Online (Sandbox Code Playgroud)

我想使用这个自定义属性作为实体添加函数的声明,我想将一个变量从函数传递给属性构造函数.可以这样做吗?

[EntityChecker(entityId)]
public int AddNewEntity(entityId)
{
 // logic of entity addition
}
Run Code Online (Sandbox Code Playgroud)

c# attributes

13
推荐指数
1
解决办法
1万
查看次数

标签 统计

attributes ×1

c# ×1