The reflection code below returns:
System.Collections.Generic.IList`1[TestReflection.Car] Cars
How can I get the Cars
root type through reflection? Not IList<Car>
- how can I get Car
?
using System;
using System.Reflection;
using System.Collections.Generic;
namespace TestReflection
{
class MainClass
{
public static void Main(string[] args)
{
Type t = typeof(Dealer);
MemberInfo[] mi = t.GetMember("Cars");
Console.WriteLine("{0}", mi[0].ToString());
Console.ReadLine();
}
}
class Dealer
{
public IList<Car> Cars { get; set; }
}
class Car
{
public string CarModel { get; set; }
}
}
Fundamentally parsing is necessary because different entities need the data to be in different forms. Parsing allows to transform data in a way that can be understood by a specific software. The obvious example are programs: they are written by humans, but they must be executed by computers.
There are two types of Parsing: The Top-down Parsing. The Bottom-up Parsing.
Ans: Parsing (also known as syntax analysis) can be defined as a process of analyzing a text which contains a sequence of tokens, to determine its grammatical structure with respect to a given grammar.
Parser is a compiler that is used to break the data into smaller elements coming from lexical analysis phase. A parser takes input in the form of sequence of tokens and produces output in the form of parse tree. Parsing is of two types: top down parsing and bottom up parsing.
The easiest way would be to produce a PropertyInfo
that represented the property in question and then its underlying type via PropertyInfo.PropertyType
. Then it's just a matter of retrieving the type arguments for this generic type, for which you could use Type.GetGenericArguments
.
Type carsElementType = typeof(Dealer)
.GetProperty("Cars")
.PropertyType // typeof(IList<Car>)
.GetGenericArguments() // new[] { typeof(Car) }
.Single(); // typeof(Car)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With