c# - 没有参数定义但仍然接受参数的构造函数

标签 c# constructor attributes

我有一个类调用下面的属性 (RequireTransaction),它接受一个参数,但在类定义中,构造函数的定义没有任何参数。当没有用一个定义的构造函数时,这个属性构造函数如何能够接受一个参数?

[RequiresTransaction(IncludeData = true)]
public ActionResult DoSomething()
{
  return something;
};


public class RequiresTransactionAttribute
{
    public bool IncludeData { get; set; }

    public RequiresTransactionAttribute()
    {
        IncludeData = false;
    }
}

最佳答案

引用the documentation :

Named or optional parameters correspond to either properties or fields of the attribute.

这里的命名参数对应于IncludeData属性,因为是命名参数,所以不需要相应的构造函数参数。

关于c# - 没有参数定义但仍然接受参数的构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56764883/

相关文章:

c# - Asp 文本框 xhtml 和额外的换行符

C# 在计时器中添加和删除事件

java - 在构造函数中使用多个参数的性能

c++ - 如何将 C++ 编译器错误 "cannot convert ' Type' 修复为 'const Type*' "?

c# - 读取方法属性的值

magento - 如何获得属性集名称?

C#:为什么这个对象在函数执行后没有被修改?

c# - 尝试实现主明细表时出错

reactjs - 从父组件更新 prop 时,不会调用 React 构造函数

javascript - Jquery获取元素属性的值并查看它是否等于不同属性的值