[英]Convert c# by-reference type to the matching non-by-reference type
我使用反射檢查C#方法的參數。 該方法具有一些out參數,對於這些參數,我可以返回類型,它們具有IsByRef = true。 例如,如果參數聲明為“輸出字符串xxx”,則該參數的類型為System.String&。 有沒有一種方法可以將System.String&轉換回System.String? 當然,該解決方案不僅應適用於System.String,還應適用於任何類型。
演示:
using System;
using System.Reflection;
class Test
{
public void Foo(ref string x)
{
}
static void Main()
{
MethodInfo method = typeof(Test).GetMethod("Foo");
Type stringByRef = method.GetParameters()[0].ParameterType;
Console.WriteLine(stringByRef);
Type normalString = stringByRef.GetElementType();
Console.WriteLine(normalString);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.