私のモデルでIEnumerableプロパティのプロパティ名を取得できません。私はTModelクラスからネストされたIEnumerablesを取得することはできません。私はいくつかの反射例を見てきましたが、これらの線に沿ったものは全くありませんでした。入れ子になったIEnumerableからの再帰プロパティ情報<Model>
私はそれぞれのネストされたモデルのIEnumerableプロパティ名を取得し、プロパティ名をリストに送信するだけです。実際の値は重要ではありません。
ご協力いただければ幸いです。
// TModel = DataContent in this context.
public class GetModelBase<TModel>
{
public string Error { get; set; }
public IEnumerable<TModel> DataContent { get; set; }
}
public class DataContent
{
public int Total { get; set; }
public IEnumerable<Data> Data { get; set; }
}
public class Data
{
public int DataId{ get; set; }
IEnumerable<DataInformation> DataInformation{ get; set; }
}
public IEnumerable<GetModelBase<TModel>> ResponseAsList<TModel>()
{
// ResponseBody in this context is a string representation of json of the models above...
var toArray = new ConvertJsonArray<GetModelBase<TModel>>(ResponseBody).ReturnJsonArray();
}
// T = GetModelBase<DataContent> in this context.
public class ConvertJsonArray<T>
{
public ConvertJsonArray(string responseString)
{
_responseString = responseString;
Convert();
}
public void Convert()
{
var result = JObject.Parse(_responseString);
// This is where I am having trouble... I am unable to get the nested IEnumerable names.
Type t = typeof(T);
PropertyInfo[] propertyInformation = t.GetProperties(BindingFlags.Public|BindingFlags.Instance);
List<string> toLists = new List<string>();
foreach (PropertyInfo pi in propertyInformation)
toLists.Add(pi.Name);
// End of Property Information Issuse...
foreach (string s in toLists.ToArray())
{
if (result[s] != null)
{
if (!(result[s] is JArray)) result[s] = new JArray(result[s]);
}
}
_jsonAsArray = result.ToString();
}
public string ReturnJsonArray()
{
return _jsonAsArray;
}
private string _responseString { get; set; }
private string _jsonAsArray { get; set; }
}
Iは上記のサンプルコードで探していた結果は、{「DataContent」、「データ」、「データ情報」}としてのみのIEnumerable名を含むリストであろう
UPDATE:
私はまだ各モデルをループするのに問題があります。私はほぼ実用的なコード例を持っています。
// This replaces the Type code in the Convert method...
GetProperties(typeof(T))
private void GetProperties(Type classType)
{
foreach (PropertyInfo property in classType.GetProperties(BindingFlags.Public | BindingFlags.Instance))
{
if (property.PropertyType.IsGenericType && (property.PropertyType.GetGenericTypeDefinition() == typeof(IEnumerable<>)))
{
ValuesToList.Add(property.Name);
foreach (Type nestedType in property.PropertyType.GetGenericArguments())
{
GetProperties(nestedType);
}
}
}
}
private List<string> ValuesToList { get; set; }
この結果は、{"DataContent"、 "Data"}を生成しますが、 "DataInformation"を取得できません。何らかの理由でIEnumerablesがforeachループ中にヒットしません。追加のヘルプは高く評価されます。
を取得しています'result'に' Error'と 'DataContent'があります。どのようにそれらのタイプのものがIEnumerable <...> 'であるかを確認するにはあなたの質問ですか? –
はい、IEnumerableプロパティ名はModelBaseだけでなく、DataContent、Data、およびDataInformationのリストにも表示する必要があります。それぞれのネストされたオブジェクトからすべての可能なIEnumerableプロパティ名を取得したい。 – user2683328
ああ、そうですね。私はこの例をコンパイル可能なものに変換するときにその再帰を殺したと思います。いずれにせよ、私は[私の答え](http://stackoverflow.com/a/43402711/1430156)で必要なフィルタ条件について説明しました。 –