gt.guybrush gt.guybrush - 1 year ago 132
C# Question

Test if a property is an ICollection in Portable class library

iam trying to test if a property of my object is a collection or not but encounter problem since in portable class library many method are not avaible

this is my method:

public virtual bool Equals(TObject other) // where TObject : class
if (other == null)
return false;

Type t = GetType();
Type otherType = other.GetType();

TypeInfo typeInfo = t.GetTypeInfo();

if (t != otherType)
return false;

IEnumerable<FieldInfo> fields = typeInfo.DeclaredFields;
foreach (FieldInfo field in fields)
{ ...

now i have to test if field is an Icollection or not but

  • ImplementedInterfaces is not avaible

  • IsAssignableFrom is not avaible

  • GetInterfaces() is not avaible

  • GetGenericTypeDefinition() is not avaible

maybe this is because i have FieldInfo and not TypeInfo?

is there something i can do?

Answer Source

i solved it excludind this type via linq Linq

IEnumerable<FieldInfo> fields = typeInfo.DeclaredFields.Where(x => x.FieldType.Name != typeof(ICollection<>).Name); 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download