在C#中函数的参数括号语法? [英] Square bracket syntax in function's parameter in C#?

查看:67
本文介绍了在C#中函数的参数括号语法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习ASP.NET和偶然发现了这个方法声明:

I'm learning ASP.NET and stumbled upon this method declaration:

public IQueryable<Product> GetProducts([QueryString("id")] int? categoryId) {.....}



该教程称的categoryId 将等于查询字符串ID(从网址,例如和ID = 5),但问题是什么是 [查询字符串(ID)] 语法叫什么名字?这是可用的ASP.NET之外,什么都会应用这样?

The tutorial said categoryId will be equal to query string "id" (From URL, like &id=5) but the question is what is [QueryString("id")] syntax called? Is this usable outside ASP.NET and what will the application of this be?

推荐答案

这是应用的 QueryStringAttribute 属性参数的categoryId 。这只是一个属性,就像那些你可能更习惯于看到的方法或类,像这样的:

That's applying the QueryStringAttribute attribute to the parameter categoryId. It's just an attribute, just like the ones you're probably more used to seeing on methods or classes, like this:

[STAThread]
static void Main()
{
}

在这种情况下,该框架的推测某一部分(我不是一个ASP.NET开发人员,所以我不能指出的究竟的东西)是使用反射来发现所有的方法,找到任何<应用于参数code> QueryStringAttribute 值,然后匹配与查询字符串名称的属性中的姓名,然后提取匹配的值传递到方法调用(再次使用反射)。

In this case, presumably some part of the framework (I'm not an ASP.NET developer, so I can't point out exactly what) is using reflection to find all the methods, find any QueryStringAttribute values applied to the parameters, and then matching the names within those attributes with the names in the query string, then extracting the matching values to pass into the method call (again using reflection).

这篇关于在C#中函数的参数括号语法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆