I have a class that looks like this :
public class ObjectA
{
public ObjectB OneObject { get; set; }
public List<ObjectC> ManyObject { get; set; }
}
And then a function to read what the class contains and return the type of property :
source = typeof(*some ObjectA*)
var classprops = source.GetProperties(BindingFlags.Public | BindingFlags.Instance)
.Where(x => x.PropertyType.IsClass && !x.PropertyType.IsValueType && x.PropertyType.Name != "String");
foreach (var prop in classprops)
{
var classnamespace = CommonTool.GetNamespaceFromProp(prop);
if ((prop.PropertyType).Namespace == "System.Collections.Generic")
{
string newprop = prop.ToString();
int start = newprop.IndexOf("[")+1;
int end = newprop.IndexOf("]");
newprop = newprop.Substring(start, end-start);
newprop = string.Format("{0}, Env.Project.Entites", newprop);
classnamespace = newprop;
}
//some code to read attributes on the properties...
}
My problem is what is within the if ((prop.PropertyType).Namespace == "System.Collections.Generic"). It smells.
Any better way to do that?
A class in the application uses List<int>.
This caused crashes.
It wasn't only smelling bad.
If you want to check if the property is a generic collection, and get the element type, you can check to see if it impelements IEnumerable<T> and get the type T if it does
Type propType = prop.PropertyType;
if (propType.IsGenericType)
{
Type enumerableType = propType.GetInterfaces().FirstOrDefault(it => it.IsGenericType && it.GetGenericTypeDefinition() == typeof(IEnumerable<>)));
if(enumerableType != null)
{
Type elementType = enumerableType.GetGenericArguments()[0];
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With