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
}根据我的理解,对于IDEnq (list of list),if (typeof(T) == typeof(List<HeaderTypeEnq<dynamic>>))应该返回false,但它返回true!
发布于 2015-11-20 17:59:04
为了检查一个类型的t是否是一个列表列表,你可以这样做:
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");发布于 2015-11-20 18:01:03
根据我的理解,if (
(T) == typeof(List>))应该为IDEnq (list of list)返回false,但它返回true!
您将像这样调用泛型类型
ValidateValue<List<HeaderTypeEnq<dynamic>>>(IDEnq, concaDel);泛型类型是此List<HeaderTypeEnq<dynamic>>。这与IDEnq没有任何关系,因为它的object。因此,当您像这样调用方法时,T恰好是List<HeaderTypeEnq<dynamic>>的类型,并且条件变为真。
你可以让你的泛型方法像这样。现在EnqTagList是T。你需要得到它的元素的类型。
private string ValidateValue<T>(T EnqTagList, ValidationDelegate del)
{
if (EnqTagList.GetType().GetGenericArguments()[0] == typeof(List<HeaderTypeEnq<dynamic>>))
{
//code
}
}https://stackoverflow.com/questions/33823602
复制相似问题