gpt4 book ai didi

.net - ParameterInfo.DefaultValue 和 ParameterInfo.RawDefaultValue 之间的区别

转载 作者:行者123 更新时间:2023-12-02 19:31:39 29 4
gpt4 key购买 nike

这是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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com