Reputation: 5539
List<List<HeaderTypeEnq<dynamic>>> IDEnq = new List<List<HeaderTypeEnq<dynamic>>>();
List<HeaderTypeEnq<dynamic>> IDListEnq = new List<HeaderTypeEnq<dynamic>>();
for (int i = 0; i < enq.Id.Count; i++)
{
IDListEnq.Add(new HeaderTypeEnq<dynamic>() { FieldTag = "ID", FieldName = "Segment Tag", Value = enq.Id[i].SegmentTag, Mandatory = "Y", CharacterType = "A", MaxLength = 03 });
IDListEnq.Add(new HeaderTypeEnq<dynamic>() { FieldTag = "01", FieldName = "ID Type", Value = enq.Id[i].IDType, Mandatory = "Y", CharacterType = "N", MaxLength = 02 });
IDListEnq.Add(new HeaderTypeEnq<dynamic>() { FieldTag = "02", FieldName = "ID Number", Value = enq.Id[i].IDNumber, Mandatory = "N", CharacterType = "P", MaxLength = 30 });
IDEnq.Add(IDListEnq);
}
ValidateValue<List<HeaderTypeEnq<dynamic>>>(IDEnq, concaDel);
private string ValidateValue<T>(object EnqTagList, ValidationDelegate del)
{
//errorstr = "";
Type typeParameterType = typeof(T);
if (typeof(T) == typeof(List<HeaderTypeEnq<dynamic>>))
{
//code
}
As per my understanding if (typeof(T) == typeof(List<HeaderTypeEnq<dynamic>>))
should return false for IDEnq
(list of list) but it returns true!
Upvotes: 1
Views: 102
Reputation: 19149
As per my understanding if (typeof(T) == typeof(List>)) should return false for IDEnq (list of list) but it returns true!
You are calling the generic type like this
ValidateValue<List<HeaderTypeEnq<dynamic>>>(IDEnq, concaDel);
Generic type is this List<HeaderTypeEnq<dynamic>>
. This has nothing to do with IDEnq
since its object
. So in when you call method like this, T
is exactly type of List<HeaderTypeEnq<dynamic>>
and the condition becomes true.
You can make your generic method like this. Now EnqTagList is T. You need to get the type of its elements.
private string ValidateValue<T>(T EnqTagList, ValidationDelegate del)
{
if (EnqTagList.GetType().GetGenericArguments()[0] == typeof(List<HeaderTypeEnq<dynamic>>))
{
//code
}
}
Upvotes: 0
Reputation: 387667
In order to check whether a type t
is a list of lists, you could do something like this:
if (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(List<>))
{
Type elementType = t.GetGenericArguments()[0];
if (elementType.IsGenericType && elementType.GetGenericTypeDefinition() == typeof(List<>))
Console.WriteLine("t is a list of lists");
else
Console.WriteLine("t is just a list");
}
else
Console.WriteLine("t is not a list");
Upvotes: 1