recursive error handler - c#

I have the following code where I'm trying to get all the properties of an object as well as the property values. Some properties can be collections or collections of collections, so I tried to set up a recursive function for those types. Unfortunately it's not working, erroring on this line
if (property.GetValue(item, null) is IEnumerable)
and I don't know what needs changed. Can anyone help here? Thanks.
public static string PackageError(IEnumerable<object> obj)
{
var sb = new StringBuilder();
foreach (object o in obj)
{
sb.Append("<strong>Object Data - " + o.GetType().Name + "</strong>");
sb.Append("<p>");
PropertyInfo[] properties = o.GetType().GetProperties();
foreach (PropertyInfo pi in properties)
{
if (pi.GetValue(o, null) is IEnumerable && !(pi.GetValue(o, null) is string))
sb.Append(GetCollectionPropertyValues((IEnumerable)pi.GetValue(o, null)));
else
sb.Append(pi.Name + ": " + pi.GetValue(o, null) + "<br />");
}
sb.Append("</p>");
}
return sb.ToString();
}
public static string GetCollectionPropertyValues(IEnumerable collectionProperty)
{
var sb = new StringBuilder();
foreach (object item in collectionProperty)
{
PropertyInfo[] properties = item.GetType().GetProperties();
foreach (var property in properties)
{
if (property.GetValue(item, null) is IEnumerable)
sb.Append(GetCollectionPropertyValues((IEnumerable)property.GetValue(item, null)));
else
sb.Append(property.Name + ": " + property.GetValue(item, null) + "<br />");
}
}
return sb.ToString();
}

I would suggest using an existing serialization mechanism, such as XML serialization or JSON serialization, to provide this information if you are trying to make it generic.

It sounds like that particular property is an indexer, so it's expecting you to pass index values to the GetValue method. There is no easy way to take an indexer and determine what values are valid to pass as indices, in the general case, as a class is free to implement an indexer however it wants. For instance, a Dictionary keyed on a string has an indexer by key, which can take indices as enumerated in the Keys property.
The typical approach to serializing collections is to handle them as a special case, treating each primitive collection type (array, List, Dictionary, etc.) individually.
Note that there is a difference here between a property returning IEnumerable and an indexer.

Related

How to dynamically get the instance of an object's property using reflection

I have found lots of examples that ALMOST tell me what I need to know. But everything so far assumes I already have an instance of the property that I want to set the value. But I don't have an instance. I have a PropertyInfo object. I can dynamically obtain the name of the property but in order to call SetValue() I must have an instance of the property to pass to the method. How do I get an instance of the property whose value I need to set? Here is my code with ??? where an instance of the property must be provided. How do I get an instance of the property and not just a PropertyInfo object?
(The reason I am writing this method is because I cannot guarantee which columns various stored procedures will return.)
protected new void MapDbResultToFields(DataRow row, DataColumnCollection columns)
{
Console.WriteLine("Entered Clinician.MapDbResultToFields");
var properties = this.GetType().GetProperties();
Console.WriteLine("Properties Count: " + properties.Length);
foreach (DataColumn col in columns)
{
Console.WriteLine("ColumnName: " + col.ColumnName);
}
foreach (var property in properties)
{
string propName = property.Name.ToLower();
Console.WriteLine("Property name: " + propName);
Console.WriteLine("Index of column name: " + columns.IndexOf(propName));
Console.WriteLine("column name exists: " + columns.Contains(propName));
if (columns.Contains(propName))
{
Console.WriteLine("PropertyType is: " + property.PropertyType);
switch (property.PropertyType.ToString())
{
case "System.String":
String val = row[propName].ToString();
Console.WriteLine("RowColumn Value (String): " + val);
property.SetValue(???, val, null);
break;
case "System.Nullable`1[System.Int64]":
case "System.Int64":
Int64.TryParse(row[propName].ToString(), out var id);
Console.WriteLine("RowColumn Value (Int64): " + id);
property.SetValue(???, id, null);
break;
case "System.Boolean":
Boolean.TryParse(row[propName].ToString(), out var flag);
Console.WriteLine("RowColumn Value (Boolean): " + flag);
property.SetValue(???, flag, null);
break;
}
}
else
{
Console.WriteLine("Property name not found in columns list");
}
}
}
You mistakenly believed you needed an instance of the property you were trying to set, but actually you need an instance of the object upon which you want to set the property. A property has no life outside of an object to which it belongs.
property.SetValue(this, val, null);
Is most likely what you were looking for.
Since you are getting the properties of THIS.. you actually have an instance of the object you are trying to set. Just use the THIS keyword when you set it.
When you GET the properties like this
var properties = this.GetType().GetProperties();
You SET the properties like this
foreach(var property in properties)
{
property.SetValue(this, id, null);
}
This will not work if you tried to get properties from an object you don't have an instance of.
var properties = SomeObject.GetType().GetProperties();
Hopefully this answers your questions!
Cheers

How to identify generic objects in exception logs?

I have a helper method that accepts parameters with generic types. This method can throw exceptions, which I log in order to debug later. Some objects have ids, some have names, etc., that would be useful to know when trying to debug. How can I log this information in a useful way?
Regardles if it is a generic method or not, unless your object is null, you can log the type with myObject.GetType().ToString()
Loger.log("Object type that has thrown an error is" + myObj.GetType().ToString());
EDIT:
If you would like to read more information from the object then you can read object properties like this:
Type type = myObj.GetType();
List<string> info = new List<String>();
info.Add("Object type that has thrown an error is: " + type.ToString() + Environment.NewLine);
foreach (var prop in type.GetProperties())
{
if (prop.CanRead)
{
info.Add("Name: " + prop.Name + " , Value: " + prop.GetValue(myObj) + Environment.NewLine);
}
}
Logger.log(string.Join("", info));
This should work without any problems for primitives. For Objects it depends on how the ToString method is implemented. Also note that it will not read recursively, just the direct members of the class.
This function returns a C# style type name (something like List<String> or List<Dictionary<Int32,String>>) from a type.
public static string FancyTypeName(Type type)
{
var typeName = type.Name.Split('`')[0];
if (type.IsGenericType)
{
typeName += string.Format("<{0}>", string.Join(",",
type.GetGenericArguments().Select(v => FancyTypeName(v)).ToArray())
);
}
return typeName;
}

Using Reflection Foreach through IEnumerable or PropertyInfo[]

I've never worked with Reflection but I have a situation where I think it would be useful without being too expensive as a process. I have five different List<.object> where each one has different properties and I have a WebMethod that returns only one of these types of List<.object>'s based on a switch statement and passes the resulting list to a method that should iterate through the values and write them out to a StringBuilder that is passed back to the WebMethod. I did not want to have to write five different methods to iterate through each object and write out the string.
Looking through SO resulted in this code as a good start for the method (below) however, I don't understand the best way to get what I need after that. I tried using PropertyInfo[] but it threw errors while casting the object. IEnumerable doesn't throw errors but I can't seem to access the values either.
private StringBuilder generateString<T>(T obj) where T : class
{
//Trying to use PropertyInfo[] example
Type type = obj.GetType();
PropertyInfo[] properties = type.GetProperties();
foreach(PropertyInfo p in properties)
sb.Append("{0},{1}", p.Name, p.GetValue(obj,null));
//Trying to use IEnumerable example
IEnumerable enumerable = (IEnumerable)obj;
foreach(var x in enumerable)
sb.Append(x);
}
When I try to look at the values in the PropertyInfo[] array it shows me that it has three items, the last being my object type but I can't seem to find any of the actual values. It throws two errors TargetParameterCountException and TargetInvocationException.
However, when I look at the IEnumerable data of x it shows me what I expect to see except not in a format where I think can access the individual pieces, for example:
x
{Base.DataType.MyObject}
Age: 24
Name: Helen Smith
Cost: 24.00
Date: 2/25/2012
Which is the correct way to try and access this data? And how do I do it correctly?
It seems that you are passing an IEnumerable, not a single object.If so you need to iterate over the objects and get the type of each item, and properties separately:
if(obj is IEnumerable)
{
foreach(var item in (IEnumerable)obj)
{
Type type = item.GetType();
PropertyInfo[] properties = type.GetProperties();
foreach(PropertyInfo p in properties)
sb.Append("{0},{1}", p.Name, p.GetValue(item,null));
}
}
What exactly do you mean by which is the best way to access the data? Are you just trying to access what is in the StringBuilder object or are you just needing to output the information in some form?
The way I have used reflection in the past to get the name and the value would be like this:
public StringBuilder generateString(List<object> objList)
{
StringBuilder sb = new StringBuilder();
int itemNumber = 0;
foreach(var obj in objList)
{
foreach (var prop in obj.GetType().GetProperties())
{
// i used the new line characters to increase readability for
// output to a console so just ignore them.
sb.Append("Item " + itemNumber);
sb.Append("\n");
sb.Append(prop.Name + ", " + prop.GetValue(obj, null));
sb.Append("\n");
}
}
return sb;
}
Viewing the output of this string builder object i saw this:
Item 1
Property1, value
Property2, value
Property3, value
Item 2
Property1, value
Property2, value
Property3, value
etc...
I hope this helped you.
If all the objects have the same type, which I believe is what you are doing. And you are passing in a List<T>, you can get the item type like so:
var listType = obj.GetType();
var itemType = listType.GetElementType();
That would give you itemType of MyObject if you passed in a List<MyObject>. From there you should be able to GetProperties as you were.
If the list is heterogeneous, then you will need to loop through and get the properties of each item as in #Selman22's answer.

C# Determine the type of a generic list

I have a C# method that I need to pass various lists into. The type of lists will be different very often. The method itself is in a different dll and cannot know the class objects contained in the lists (no references, no using statements). The method needs to read each of the members of each of the items in a list and build and return a string based off of that.
My question is how can get the metadata for for these various objects at compiletime / runtime? Will reflection do that?
Also, once I get the metadata, how do i use it to actually use the variables?
Thank you
EDIT: currently I am using it like the following:
public string GetData(List<User> list){
//...
List<RowData> rows = new List<RowData>();
foreach (User item in list)
{
RowData row = new RowData();
row.id = count++;
row.cell = new string[3];
row.cell[0] = item.ID.ToString();
row.cell[1] = item.name;
row.cell[2] = item.age.ToString();
rows.Add(row);
}
//...
return new JavaScriptSerializer().Serialize(rows.ToArray());
Right now this is very specific. I want to replace this with generics so I can pass items other than "User"
If the parameter (will call it list for discussion's sake) coming into the method is List<T> then you can do the following:
Type type = list.GetType().GetGenericArguments()[0];
If you just have a generic T class, then you can always just go with this:
Type typeParameterType = typeof(T);
.GetType() will get you a Type and you can get a lot of descriptions about the type.
You can also discover members of the instances you have using reflection
Please be precise with what you want more exactly ?
EDIT : Here is a way, try making it an extenstion method it would be better
public static List<RowData> ToDataTable<T>(IEnumerable<T> source)
{
System.Reflection.PropertyInfo[] properties = typeof(T).GetProperties();
List<RowData> rows = new List<JQGridRow>();
foreach (var item in source)
{
RowData row = new RowData();
row.cells = new string[properties.Length];
int i=0;
foreach (var prop in properties)
{
row.cells[i] = prop.GetValue(item, null);i++;
}
rows.Add(row);
}
return rows;
}
Yes, you can use reflection to obtain the type of an object at runtime.
Simple call to object.GetType() will give you the type of this instance.
What you do with this information is up to you.
Maybe something like this?
public string GetData<T>(IEnumerable<T> list, Func<T, RowData> fillRow)
{
List<RowData> rows = new List<JQGridRow>();
foreach (User item in list)
{
RowData row = new RowData();
row.id = count++;
fillRow(row);
rows.Add(row);
}
//...
return new JavaScriptSerializer().Serialize(rows.ToArray());
}
And use it like
string s = GetData(users, row =>
{
row.cell = new string[3];
row.cell[0] = item.ID.ToString();
row.cell[1] = item.name;
row.cell[2] = item.age.ToString();
});
This might work if you treat them all as objects, using the intrinsic ToString() method for your string building.

C# - Reflection using Generics: Problem with Nested collections of ILists

I would like to be able to print object properties, and I've hit a snag when I hit nested collections of the iLists.
foreach (PropertyInformation p in properties)
{
//Ensure IList type, then perform recursive call
if (p.PropertyType.IsGenericType)
{
// recursive call to PrintListProperties<p.type?>((IList)p," ");
}
Can anyone please offer some help?
Cheers
KA
I'm just thinking aloud here. Maybe you can have a non generic PrintListProperties method that looks something like this:
private void PrintListProperties(IList list, Type type)
{
//reflect over type and use that when enumerating the list
}
Then, when you come across a nested list, do something like this:
if (p.PropertyType.IsGenericType)
{
PringListProperties((Ilist)p,p.PropertyType.GetGenericArguments()[0]);
}
Again, haven't tested this, but give it a whirl...
foreach (PropertyInfo p in props)
{
// We need to distinguish between indexed properties and parameterless properties
if (p.GetIndexParameters().Length == 0)
{
// This is the value of the property
Object v = p.GetValue(t, null);
// If it implements IList<> ...
if (v != null && v.GetType().GetInterface("IList`1") != null)
{
// ... then make the recursive call:
typeof(YourDeclaringClassHere).GetMethod("PrintListProperties").MakeGenericMethod(v.GetType().GetInterface("IList`1").GetGenericArguments()).Invoke(null, new object[] { v, indent + " " });
}
Console.WriteLine(indent + " {0} = {1}", p.Name, v);
}
else
{
Console.WriteLine(indent + " {0} = <indexed property>", p.Name);
}
}
p.PropertyType.GetGenericArguments()
will give you an array of the type arguments. (in ths case, with just one element, the T in IList<T>)
var dataType = myInstance.GetType();
var allProperties = dataType.GetProperties(BindingFlags.Public | BindingFlags.Instance);
var listProperties =
allProperties.
Where(prop => prop.PropertyType.GetInterfaces().
Any(i => i == typeof(IList)));

Categories