- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是How do I get default values of optional parameters?的后续问题
来自documentation, DefaultValue :
Gets a value indicating the default value if the parameter has a default value.
This property is used only in the execution context. In the reflection-only context, use the RawDefaultValue property instead.
The default value is used when an actual value is not specified in the method call. A parameter can have a default value that is null. This is distinct from the case where a default value is not defined.
来自documentation, RawDefaultValue :
Gets a value indicating the default value if the parameter has a default value.
This property can be used in both the execution context and the reflection-only context.
The default value is used when an actual value is not specified in the method call. A parameter can have a default value that is null. This is distinct from the case where a default value is not defined.
文档非常相似,只是一个用于反射上下文,而另一个则不。那有什么区别呢?什么时候在没有反射的情况下使用 DefaultValue
?我的意思是我们如何在没有反射的情况下获得默认值?我错过了什么吗?
更新
我创建了两个这样的重载:
public void Required(string value)
{
}
public void Optional(string value = "", int i = -1)
{
}
我测试过:
var f = requiredInfo.GetParameters().Select(p => p.DefaultValue).ToArray();
var g = requiredInfo.GetParameters().Select(p => p.RawDefaultValue).ToArray();
var h = optionalInfo.GetParameters().Select(p => p.DefaultValue).ToArray();
var i = optionalInfo.GetParameters().Select(p => p.RawDefaultValue).ToArray();
//f equals g and h equals i in every way!
那么,鉴于我的测试显示(全部在反射上下文中)没有任何区别,有什么区别?
最佳答案
“在反射上下文中”和“仅反射上下文”之间存在微妙但显着的区别。 “仅反射上下文”指的是非常具体的东西:
这是一种加载程序集仅供检查的方法,具有不需要加载甚至不需要任何依赖程序集的明显优点。
由于您似乎有意执行您正在反射的某些代码,因此仅反射上下文对您的用处有限。
关于.net - ParameterInfo.DefaultValue 和 ParameterInfo.RawDefaultValue 之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16185826/
考虑以下示例: class Program { static void Main(string[] args) { foreach(var par in typeof(
这是How do I get default values of optional parameters?的后续问题 来自documentation, DefaultValue : Gets a va
我是一名优秀的程序员,十分优秀!