Recursive PropertyInformation from Nested IEnumerable<Model> - c#

I am having trouble getting the property Names of the IEnumerable properties in my models. I cant seem to get the Nested IEnumerables from the TModel classes. I have looked into some reflection examples but haven't something quite along these lines.
I am looking to just get the IEnumerable property names for each nested model and send the property name to a list. The actual value is not important.
Any help would be greatly appreciated.
// 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; }
}
The result I am looking for in the above code sample would be a list containing only the IEnumerable names as such { "DataContent", "Data", "DataInformation" }
UPDATE:
I am still having trouble looping through each model. I have a nearly working code example.
// 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; }
The results for this yields { "DataContent", "Data" } but fails to get "DataInformation". For some reason the IEnumerables are not hit while in the foreach loop. Additional help would be appreciated.

You already have the PropertyInfo, so you are almost there - all that is left is to recognize which properties are of type IEnumerable<...>, where ... can be an arbitrary type.
For this purpose, check the PropertyType property.
It is a Type instance for which you can check whether it is based upon the generic type definition IEnumerable<T> by means of the GetGenericTypeDefinition method.
That method will throw an exception for non-generic types, so you will also have to check IsGenericType:
if (pi.PropertyType.IsGenericType
&& (pi.PropertyType.GetGenericTypeDefinition() == typeof(IEnumerable<>)))
{
toLists.Add(pi.Name);
}

Related

Initializing (list) properties in constructor using reflection

I am trying to initialize all properties in class (lists) with using reflection:
public class EntitiesContainer
{
public IEnumerable<Address> Addresses { get; set; }
public IEnumerable<Person> People { get; set; }
public IEnumerable<Contract> Contracts { get; set; }
public EntitiesContainer()
{
var propertyInfo = this.GetType().GetProperties();
foreach (var property in propertyInfo)
{
property.SetValue(property, Activator.CreateInstance(property.GetType()), null);
}
}
}
I am getting exception:
No constructor has been defined for this object without parameters.
I would appreciate tips.
You can do this provided that you define the properties as concrete types. This actually works:
public class EntitiesContainer
{
public List<Address> Addresses { get; set; }
public List<Person> People { get; set; }
public List<Contract> Contracts { get; set; }
public EntitiesContainer()
{
var propertyInfo = this.GetType().GetProperties();
foreach (var property in propertyInfo)
{
property.SetValue(this, Activator.CreateInstance(property.PropertyType));
}
}
}
You cannot create an instance of an IEnumerable<T> because it's an interface.
But why would you want to to this? You'd better initialize the properties using the auto-property initializer that was introduced in C#6:
public class EntitiesContainer
{
public IEnumerable<Address> Addresses { get; set; } = new List<Address>;
public IEnumerable<Person> People { get; set; } = new List<Address>;
public IEnumerable<Contract> Contracts { get; set; } = new List<Address>;
}
In general here, the type of object you want to create is property.PropertyType; and the object upon which you want to set the value is this, so:
property.SetValue(this, Activator.CreateInstance(property.PropertyType), null);
But! your properties are IEnumerable<T>, not List<T> - can't create an interface, only a concrete type. So you'd have to do a lot of work with deconstructing the generic IEnumerable<Foo> to Foo (var args = type.GetGenericTypeArguments()) and constructing a List<Foo> (typeof(List<>).MakeGenericType(args)). Or just change the property types to List<T>!
Frankly, it would be easier to just do:
public IEnumerable<Address> Addresses { get; set; } = new List<Address>();
public IEnumerable<Person> People { get; set; } = new List<Person>();
public IEnumerable<Contract> Contracts { get; set; } = new List<Contract>();
or:
public List<Address> Addresses { get; } = new List<Address>();
public List<Person> People { get; } = new List<Person>();
public List<Contract> Contracts { get; } = new List<Contract>();
To sum up what I wanted to acheive was method called in constructor like below:
private void InitializeAllCollections()
{
var properties = this.GetType().GetProperties();
foreach (var property in properties)
{
var genericType = property.PropertyType.GetGenericArguments();
var creatingCollectionType = typeof(List<>).MakeGenericType(genericType);
property.SetValue(this, Activator.CreateInstance(creatingCollectionType));
}
}
Thanks guys for your help. :)
I had a similar need: when creating business objects for unit tests, I want to default all uninitialized Lists to new Lists, so that if a test needs to add something to a list, I don't have to worry about initializing it there. And like the OP, I have too many business objects to change them all to default. My solution is a mix of the others; the exceptions being I only want List properties, and only if they are not yet initialized:
public static T DefaultLists<T>(this T obj)
{
var properties = obj.GetType().GetProperties().Where(q => q.PropertyType.Name == "List`1" && q.GetValue(obj) == null);
foreach(var property in properties)
property.SetValue(obj, Activator.CreateInstance(property.PropertyType));
return obj;
}
Now my sample object creator can return new businessObject.DefaultLists();

Iterating through a generic list property

So I have a class
public class SearchViewModel<T> where T:class
{
public string LastSortColumn { get; set; }
public string LastSortDirection { get; set; }
public List<T> SearchItems;
public SearchViewModel(List<T> SearchItems)
{
this.SearchItems = SearchItems;
}
}
Is there any way that I can iterate through the property SearchItems with a foreach loop or something? PS: SearchViewModel is also my model
Use var here, the compiler than will decide in the foreach loop from which type the SearchItem in the list is. In this example var is of type string/
SearchViewModel<string> vs = new SearchViewModel<string>(new List<string> { "1","2","3"});
foreach (var item in vs.SearchItems)
{
// logic, item in this case is string
}

Replace Placeholders in Html with Model Object

I have a hard time explaining what I'm exactly trying to do. There's probably a name for it, but I don't know what it is.
First, I have a model such as:
public class Customer
{
public int Id { get; set; }
public int ProductId { get; set; }
...more properties...
public virtual Product Product { get; set; }
}
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
...more properties...
}
Second, I have a string of HTML text with placeholders in {}. I'd like to have something like {Id} and have it replace the Html text with the model properties.
<div><span>Name</span><span>{Id}-{Product.Name}</span></div>
My thought was to use a NameValueCollection to get the Model properties as strings. Using reflection, I can do that for the base properties, but not for something like Product.Name.
Am I going about this the wrong way? What could I use to get a NameValueCollection that I could loop through and do a replace of the Html?
Here is the current code I have (skips virtual properties):
public virtual NameValueCollection GetNameValueCollection(Object obj)
{
Type type = obj.GetType();
PropertyInfo[] properties = type.GetProperties();
var coll = new NameValueCollection();
foreach (PropertyInfo property in properties)
{
if(!property.GetGetMethod().IsVirtual)
{
if (property.PropertyType == typeof(DateTime))
{
var date = (DateTime)property.GetValue(obj, null);
coll.Add(property.Name, date.ToLongDateString());
}
else if (property.PropertyType == typeof(DateTime?))
{
var date = (DateTime?)property.GetValue(obj, null);
if (date.HasValue)
{
coll.Add(property.Name, date.Value.ToLongDateString());
}
else
{
coll.Add(property.Name, string.Empty);
}
}
else
{
var value = property.GetValue(obj, null);
if (value != null)
{
coll.Add(property.Name, value.ToString());
}
}
}
}
return coll;
}
This should be recursive, but it seems like there should be a better way. By the way, I don't need a NameValueCollection specifically (could be Dictionary<string,string> for example). Thoughts? Is there a nuget package that already does this?
I ended up just using what I had and added a sub section for handling child objects. I didn't want to do full recursion since I only wanted the direct child objects, not all the way through the chain.

Is Class SubClass or Object

Note: I'm asking about subclasses, not derived classes.
Basically, what I need to do is check properties of an object and look for those that have a specific attribute set.
The problem I have is that a lot of the properties are from subclasses
public class ExampleAttribute : Attribute
{
public object Whatever { get; set; }
}
public class MiddleEarth
{
[Example]
public Type EntityType { get; set; }
}
public class Elf : MiddleEarth
{
[Example]
public SubClass ItsLateAndImTired { get; set; }
public IList<Arg> Args { get; set; }
//Need to check properties of this object as well
public class SubClass
{
public object SubProperty { get; set; }
[Example]
public object SubPropertyWithAttribute { get; set; }
}
public class Arg
{
[Example]
public string Something { get; set; }
}
}
Now, I'm trying to do it as follows...but for reasons noted in the comments it won't work
public List<string> IterateProperties(object _o)
{
List<string> problems = new List<string>();
foreach (PropertyInfo info in _o.GetType().GetProperties())
{
//All 3 of these will return the exact same thing
Type thisType = this.GetType();
Type oType = _o.GetType();
Type infoType = info.ReflectedType;
//IsSubClassOf only checks for derived classes,
//so it's not the method I'm looking for
if (info.ReflectedType.IsSubclassOf(this.GetType()))
{
object sub = info.GetValue(_o, null);
if (sub != null)
{
problems.AddRange(this.IterateProperties(sub));
}
}
object[] attributes = info.GetCustomAttributes(typeof(ExampleAttribute), true);
foreach (object o in attributes)
{
if (info.GetValue(_o, null) == null)
{
problems.Add(String.Format("Attribute {0} in class {1} cannot be null", info.Name, info.ReflectedType.ToString()));
}
}
}
return problems;
}
Any ideas?
I believe what you're looking for is Type.GetNestedTypes()
http://msdn.microsoft.com/en-GB/library/493t6h7t.aspx
I'm not sure, but think that GetProperties method got some flags that can help...

dynamic Getvalue return object cast to PropertyInfo.PropertyType

My question is: How can make
PropertyInfo.GetValue(object, null);
return value with casted to PropertyType, not return object.
I tried Convert.ChangeType but not luck.
Thank you.
Update 1:
More detail:
My code:
foreach (var propertyInfo in customerInfo.CustomerRelationship.GetType().GetProperties())
{
var relationshipList = (IList)propertyInfo.GetValue(customerInfo.CustomerRelationship, null);
foreach (var relationship in relationshipList)
{
}
}
with
public struct CustomerInfo
{
public Customer CustomerItem { get; set; }
public Relationship CustomerRelationship { get; set; }
}
public class Relationship
{
public List<Contact> ContactItems { get; set; }
public List<Department> DepartmentItems { get; set; }
..........
}
Because i can't dynamic cast each Relationship Item show i can't compare, query (Linq) manipulate with database .
You can't.
For the sake of simplicity, a generic wrapper can be written over it.
public static T GetValue<T>(Object obj1, Object obj2)
{
return (T)PropertyInfo.GetValue(obj1, obj2);
}

Categories

Resources