モデルリフレクションツールを作ろうとしています。私はこれまで長い道のりを歩んできましたが、今は立ち往生しています。
私はこれを持っています
public static void RenderModelList(List<T> modelList)
{
foreach (T model in modelList)
{
PropertyInfo[] properties = model.GetType().GetProperties();
foreach (PropertyInfo property in properties)
{
object propValue = property.GetValue(model, null);
//Check if the property is a collection and do recursion
if (propValue != null)
{
if (isCollection(propValue))
{
//This only works for Lists of the same <T>
List<T> li = Convert.ChangeType(propValue, propValue.GetType()) as List<T>;
if (li != null)
{
if (li.Count > 0)
{
RenderModelList(li, loop);
}
}
else
{
//Its another type what to do?
// Create a List<> of unknown type??
}
}
}
}
}
}
私の問題は、このメソッド a を渡しList<Persons>
、Person に a のプロパティがあるList<Cars>
場合です。これは T ではないため、Convert.ChangeType を使用できません。
では、「リスト」をループして、このオブジェクトのプロパティにアクセスするにはどうすればよいでしょうか。