I can understand the Type.GetType()
's Scenario which the Object's type will be got, but what is Type.GetElementType()
and what does it do?
Can anyone explain it in a clear way?
Type.GetElementType
is used for arrays, pointers, and by-ref parameter types. For example:
object value = new int[100];
Console.WriteLine(value.GetType()); // System.Int32[]
Console.WriteLine(value.GetType().GetElementType()); // System.Int32
Or:
public void Foo(ref int x) {}
...
var method = typeof(Test).GetMethod("Foo");
var parameter = method.GetParameters()[0];
Console.WriteLine(parameter.ParameterType); // System.Int32&
Console.WriteLine(parameter.ParameterType.GetElementType()); // System.Int32
As for when you'd use them - well, it depends what you're using reflection for to start with.
See more on this question at Stackoverflow