C#知识点-反射
收纳一些常用的C#知识点
收纳归总一些常用的C#技巧或者知识点
Type.IsByRef
当
参数
的类型为引用传递
时,IsByRef为True。此时要获得最终类型需要调用GetElementType()。
private class CustomTypeClass
{
//Console:type name:Int32 isByRef:False
public void Fun(int value) { }
//Console:type name:Int32& isByRef:True elementType:Int32
public void Fun(ref int value) { }
//Console:type name:Object isByRef:False
public void Fun(object obj) { }
//Console:type name:Object& isByRef:True elementType:Object
public void Fun(ref object obj) { }
//Console:type name:Int32[] isByRef:False
public void Fun(int[] intarray) { }
//Console:type name:Int32[]& isByRef:True elementType:Int32[]
public void Fun(ref int[] intarray) { }
}
static void Main(string[] args)
{
MethodInfo[] methodInfos = typeof(CustomTypeClass).GetMethods();
foreach (var methodInfo in methodInfos)
{
ParameterInfo[] parameters = methodInfo.GetParameters();
foreach (var parameter in parameters)
{
var parameterType = parameter.ParameterType;
Debug.WriteLine(string.Format("method name:{0}", methodInfo.Name));
LogType(parameterType);
}
}
}
private static void LogType(Type type)
{
if (type.IsByRef)
{
Debug.WriteLine(string.Format("type name:{0} isByRef:{1} elementType:{2}", type.Name, type.IsByRef, type.GetElementType().Name));
}
else
{
Debug.WriteLine(string.Format("type name:{0} isByRef:{1}", type.Name, type.IsByRef));
}
}
评论
还沒有留言。