My problem is quite simple
Suppose I have those class:
public class A
{
public Collection<B> B { get; set; }
public Collection<C> C { get; set; }
}
public class B
{
public int IntB { get; set; }
}
public class C
{
public string StringC { get; set; }
}
And I write a function:
public void GetValue(string fieldName){
A a = new A();
PropertyInfo infor = typeof(A).GetProperty(fieldName);
object obj = infor.GetValue(a,null);
}
My question is how can I turn obj to corresponding Collection, in this case is Collection<B> or Collection<C>, depending in fieldName value
Thank in advance
You can cast it:
EDIT:
if you use LINQ on the resulting collections, you may want to use OfType (link: http://msdn.microsoft.com/en-us/library/bb360913.aspx) and/or Cast (link: http://msdn.microsoft.com/en-us/library/bb341406.aspx)
Like