我想我不明白Type.IsByRef.NET的属性应该表明什么.我认为它应该为引用类型返回true而对值类型返回false,因此与Type.IsValueType属性相反.我不能让它为显然是引用类型的类型返回true.这是一个例子:
using System.Text;
public class Program
{
static void Main(string[] args)
{
try
{
int i = 0;
Console.WriteLine(i.GetType().IsByRef); // returns false - OK
Exception e = new Exception();
Console.WriteLine(e.GetType().IsByRef); // returns false - ??
StringBuilder sb = new StringBuilder();
Console.WriteLine(sb.GetType().IsByRef); // returns false - ??
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
Console.ReadKey(true);
}
}
Run Code Online (Sandbox Code Playgroud)
我在这里错过了什么?
IsByRef 对于通过引用传递的参数为true:
public void Foo(ref int x) { }
...
var fooMethod = this.GetType().GetMethod("Foo");
var param = fooMethod.GetParameters()[0];
bool isByRef = param.ParameterType.IsByRef; // true
Run Code Online (Sandbox Code Playgroud)
它与值类型和引用类型无关.要检查类型是否为值类型,请检查IsValueType(对于值类型,返回true,否则返回false).
| 归档时间: |
|
| 查看次数: |
249 次 |
| 最近记录: |