繁体   English   中英

从 C# 中的另一个类访问 Controller 类的属性

[英]Accessing property of Controller class from another class in C#

我的代码可能有点冗长但很容易理解,因为我已尽力解释它,所以请与我交谈。 我有一个控制器类:

public class QueryController : ControllerBase
    {
        private readonly ILogger<QueryController> log;
        public bool ISReadCol { get; set; } //Focus on this part

        public QueryController(ILogger<QueryController> logger)
        {
            this.log = logger;           
        }
        
        [HttpGet("/api/v2/{database}/Tables/{table}")]
        public async Task<IActionResult> GetColFields(
             [FromRoute] string database,
             [FromRoute] string table,
             CancellationToken cancel)
        {
            //some code

            ISReadCol = true;   //setting property to true    
            return await GetQueryResult(database);          
        }        
        
         private async Task<IActionResult> GetQueryResult(string database)
        {
           //some code
           
           return new QueryResult(pool, log);  //[1]
        }
    }

现在,我想访问“QueryResult”类中的属性“ISReadCol”。

“QueryResult.cs”如下:

class QueryResult : ActionResult
    {
        private readonly ILogger log;
        private readonly ConnectionPoolEntry poolEntry;
        
        public QueryResult(ConnectionPoolEntry poolEntry, ILogger log)
        {           
            this.log = log;            
            this.poolEntry = poolEntry;            
        }
        
        public override async Task ExecuteResultAsync(ActionContext context)
        {   
            **//HOW CAN I ACCESS THE "ISReadCol" property here???.** 
        }
    }

如果我在“QueryResult”构造函数中传递“QueryController”实例,例如:

private readonly QueryController QR;

public QueryResult(ConnectionPoolEntry poolEntry, ILogger log, QueryController QR)
{           
    this.log = log;            
    this.poolEntry = poolEntry;   
    this.QR = QR;           
}
    and then QR.ISReadCol, but that doesn't work too as [1] call need to be updated too.

刚刚将“ISReadCol”作为参数传递并相应地修改了函数调用

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM