ParameterInfo.DefaultValue和ParameterInfo.RawDefaultValue的区别 [英] Difference between ParameterInfo.DefaultValue and ParameterInfo.RawDefaultValue

查看:244
本文介绍了ParameterInfo.DefaultValue和ParameterInfo.RawDefaultValue的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个后续问题<一href="http://stackoverflow.com/questions/16185124/how-do-i-get-default-values-of-optional-parameters">How做我得到的可选参数的默认值?

文档,默认值的:

获取一个指示默认值,如果该参数的   默认值。

Gets a value indicating the default value if the parameter has a default value.

此属性用于只在执行上下文。在里面   只反射上下文中,使用RawDefaultValue属性。

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.

文档,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.

该文件是如此的相似,除了一个是反射上下文和其他不。有什么不同之处呢?当过默认值使用没有反映呢?我的意思是我们怎样才能不反映一个默认值?我失去了一些东西?

The documentation is so similar except that one is for reflection context and other not. What difference is that? When is ever DefaultValue used without reflection at all? I mean how do we get a default value without reflection? Am I missing something?

更新

我创建了两个重载这样的:

I created two overloads like this:

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!

那么,什么是差给出我的测试表明(全部反射上下文)都没有区别?

So what is the difference given that my test shows (all in reflection context) no difference at all?

推荐答案

有反射的情况下之间的微妙但显著差异,只反射环境。而反思唯一方面指的是一些非常具体的:

There is a subtle but significant difference between "in the context of reflection" and "the reflection-only context". The "reflection only context" is referring to something very specific:

这是一种方式来加载只检查一个程序集,具有不需要任何相关组件的独特优势被加载,甚至是present。

It is a way to load an assembly for examination only and has the distinct advantage of not requiring any dependent assemblies to be loaded or even to be present.

既然你似乎有执行一些code你反映的每一个意图,只反射上下文将是有限的使用给你。

Since you appear to have every intention of executing some of the code you are reflecting, the reflection-only context would be of limited use to you.

这篇关于ParameterInfo.DefaultValue和ParameterInfo.RawDefaultValue的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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