I am trying implement the Data transformation using Reflection1 example in my code.
The GetSourceValue function has a switch comparing various types, but I want to remove these types and properties and have GetSourceValue get the value of the property using only a single string as the parameter. I want to pass a class and property in the string and resolve the value of the property.
Is this possible?
1 Web Archive version of original blog post
public static object GetPropValue(object src, string propName)
{
return src.GetType().GetProperty(propName).GetValue(src, null);
}
Of course, you will want to add validation and whatnot, but that is the gist of it.
How about something like this:
public static Object GetPropValue(this Object obj, String name) {
foreach (String part in name.Split('.')) {
if (obj == null) { return null; }
Type type = obj.GetType();
PropertyInfo info = type.GetProperty(part);
if (info == null) { return null; }
obj = info.GetValue(obj, null);
}
return obj;
}
public static T GetPropValue<T>(this Object obj, String name) {
Object retval = GetPropValue(obj, name);
if (retval == null) { return default(T); }
// throws InvalidCastException if types are incompatible
return (T) retval;
}
This will allow you to descend into properties using a single string, like this:
DateTime now = DateTime.Now;
int min = GetPropValue<int>(now, "TimeOfDay.Minutes");
int hrs = now.GetPropValue<int>("TimeOfDay.Hours");
You can either use these methods as static methods or extensions.
Add to any Class:
public class Foo
{
public object this[string propertyName]
{
get { return this.GetType().GetProperty(propertyName).GetValue(this, null); }
set { this.GetType().GetProperty(propertyName).SetValue(this, value, null); }
}
public string Bar { get; set; }
}
Then, you can use as:
Foo f = new Foo();
// Set
f["Bar"] = "asdf";
// Get
string s = (string)f["Bar"];
What about using the CallByName of the Microsoft.VisualBasic namespace (Microsoft.VisualBasic.dll)? It uses reflection to get properties, fields, and methods of normal objects, COM objects, and even dynamic objects.
using Microsoft.VisualBasic;
using Microsoft.VisualBasic.CompilerServices;
and then
Versioned.CallByName(this, "method/function/prop name", CallType.Get).ToString();
Great answer by jheddings. I would like to improve it by allowing referencing of aggregated arrays or collections of objects, so that propertyName could be property1.property2[X].property3:
public static object GetPropertyValue(object srcobj, string propertyName)
{
if (srcobj == null)
return null;
object obj = srcobj;
// Split property name to parts (propertyName could be hierarchical, like obj.subobj.subobj.property
string[] propertyNameParts = propertyName.Split('.');
foreach (string propertyNamePart in propertyNameParts)
{
if (obj == null) return null;
// propertyNamePart could contain reference to specific
// element (by index) inside a collection
if (!propertyNamePart.Contains("["))
{
PropertyInfo pi = obj.GetType().GetProperty(propertyNamePart);
if (pi == null) return null;
obj = pi.GetValue(obj, null);
}
else
{ // propertyNamePart is areference to specific element
// (by index) inside a collection
// like AggregatedCollection[123]
// get collection name and element index
int indexStart = propertyNamePart.IndexOf("[")+1;
string collectionPropertyName = propertyNamePart.Substring(0, indexStart-1);
int collectionElementIndex = Int32.Parse(propertyNamePart.Substring(indexStart, propertyNamePart.Length-indexStart-1));
// get collection object
PropertyInfo pi = obj.GetType().GetProperty(collectionPropertyName);
if (pi == null) return null;
object unknownCollection = pi.GetValue(obj, null);
// try to process the collection as array
if (unknownCollection.GetType().IsArray)
{
object[] collectionAsArray = unknownCollection as object[];
obj = collectionAsArray[collectionElementIndex];
}
else
{
// try to process the collection as IList
System.Collections.IList collectionAsList = unknownCollection as System.Collections.IList;
if (collectionAsList != null)
{
obj = collectionAsList[collectionElementIndex];
}
else
{
// ??? Unsupported collection type
}
}
}
}
return obj;
}
If I use the code from Ed S. I get
'ReflectionExtensions.GetProperty(Type, string)' is inaccessible due to its protection level
It seems that GetProperty() is not available in Xamarin.Forms. TargetFrameworkProfile is Profile7 in my Portable Class Library (.NET Framework 4.5, Windows 8, ASP.NET Core 1.0, Xamarin.Android, Xamarin.iOS, Xamarin.iOS Classic).
Now I found a working solution:
using System.Linq;
using System.Reflection;
public static object GetPropValue(object source, string propertyName)
{
var property = source.GetType().GetRuntimeProperties().FirstOrDefault(p => string.Equals(p.Name, propertyName, StringComparison.OrdinalIgnoreCase));
return property?.GetValue(source);
}
Source
About the nested properties discussion, you can avoid all the reflection stuff if you use the DataBinder.Eval Method (Object, String) as below:
var value = DataBinder.Eval(DateTime.Now, "TimeOfDay.Hours");
Of course, you'll need to add a reference to the System.Web assembly, but this probably isn't a big deal.
The method to call has changed in .NET Standard (as of 1.6). Also we can use C# 6's null conditional operator.
using System.Reflection;
public static object GetPropValue(object src, string propName)
{
return src.GetType().GetRuntimeProperty(propName)?.GetValue(src);
}
The below method works perfect for me:
class MyClass {
public string prop1 { set; get; }
public object this[string propertyName]
{
get { return this.GetType().GetProperty(propertyName).GetValue(this, null); }
set { this.GetType().GetProperty(propertyName).SetValue(this, value, null); }
}
}
To get the property value:
MyClass t1 = new MyClass();
...
string value = t1["prop1"].ToString();
To set the property value:
t1["prop1"] = value;
public static List<KeyValuePair<string, string>> GetProperties(object item) //where T : class
{
var result = new List<KeyValuePair<string, string>>();
if (item != null)
{
var type = item.GetType();
var properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (var pi in properties)
{
var selfValue = type.GetProperty(pi.Name).GetValue(item, null);
if (selfValue != null)
{
result.Add(new KeyValuePair<string, string>(pi.Name, selfValue.ToString()));
}
else
{
result.Add(new KeyValuePair<string, string>(pi.Name, null));
}
}
}
return result;
}
This is a way to get all properties with their values in a List.
Using PropertyInfo of the System.Reflection namespace. Reflection compiles just fine no matter what property we try to access. The error will come up during run-time.
public static object GetObjProperty(object obj, string property)
{
Type t = obj.GetType();
PropertyInfo p = t.GetProperty("Location");
Point location = (Point)p.GetValue(obj, null);
return location;
}
It works fine to get the Location property of an object
Label1.Text = GetObjProperty(button1, "Location").ToString();
We'll get the Location : {X=71,Y=27}
We can also return location.X or location.Y on the same way.
public class YourClass
{
//Add below line in your class
public object this[string propertyName] => GetType().GetProperty(propertyName)?.GetValue(this, null);
public string SampleProperty { get; set; }
}
//And you can get value of any property like this.
var value = YourClass["SampleProperty"];
The following code is a Recursive method for displaying the entire hierarchy of all of the Property Names and Values contained in an object's instance. This method uses a simplified version of AlexD's GetPropertyValue() answer above in this thread. Thanks to this discussion thread, I was able to figure out how to do this!
For example, I use this method to show an explosion or dump of all of the properties in a WebService response by calling the method as follows:
PropertyValues_byRecursion("Response", response, false);
public static object GetPropertyValue(object srcObj, string propertyName)
{
if (srcObj == null)
{
return null;
}
PropertyInfo pi = srcObj.GetType().GetProperty(propertyName.Replace("[]", ""));
if (pi == null)
{
return null;
}
return pi.GetValue(srcObj);
}
public static void PropertyValues_byRecursion(string parentPath, object parentObj, bool showNullValues)
{
/// Processes all of the objects contained in the parent object.
/// If an object has a Property Value, then the value is written to the Console
/// Else if the object is a container, then this method is called recursively
/// using the current path and current object as parameters
// Note: If you do not want to see null values, set showNullValues = false
foreach (PropertyInfo pi in parentObj.GetType().GetTypeInfo().GetProperties())
{
// Build the current object property's namespace path.
// Recursion extends this to be the property's full namespace path.
string currentPath = parentPath + "." + pi.Name;
// Get the selected property's value as an object
object myPropertyValue = GetPropertyValue(parentObj, pi.Name);
if (myPropertyValue == null)
{
// Instance of Property does not exist
if (showNullValues)
{
Console.WriteLine(currentPath + " = null");
// Note: If you are replacing these Console.Write... methods callback methods,
// consider passing DBNull.Value instead of null in any method object parameters.
}
}
else if (myPropertyValue.GetType().IsArray)
{
// myPropertyValue is an object instance of an Array of business objects.
// Initialize an array index variable so we can show NamespacePath[idx] in the results.
int idx = 0;
foreach (object business in (Array)myPropertyValue)
{
if (business == null)
{
// Instance of Property does not exist
// Not sure if this is possible in this context.
if (showNullValues)
{
Console.WriteLine(currentPath + "[" + idx.ToString() + "]" + " = null");
}
}
else if (business.GetType().IsArray)
{
// myPropertyValue[idx] is another Array!
// Let recursion process it.
PropertyValues_byRecursion(currentPath + "[" + idx.ToString() + "]", business, showNullValues);
}
else if (business.GetType().IsSealed)
{
// Display the Full Property Path and its Value
Console.WriteLine(currentPath + "[" + idx.ToString() + "] = " + business.ToString());
}
else
{
// Unsealed Type Properties can contain child objects.
// Recurse into my property value object to process its properties and child objects.
PropertyValues_byRecursion(currentPath + "[" + idx.ToString() + "]", business, showNullValues);
}
idx++;
}
}
else if (myPropertyValue.GetType().IsSealed)
{
// myPropertyValue is a simple value
Console.WriteLine(currentPath + " = " + myPropertyValue.ToString());
}
else
{
// Unsealed Type Properties can contain child objects.
// Recurse into my property value object to process its properties and child objects.
PropertyValues_byRecursion(currentPath, myPropertyValue, showNullValues);
}
}
}
public static TValue GetFieldValue<TValue>(this object instance, string name)
{
var type = instance.GetType();
var field = type.GetFields(BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance).FirstOrDefault(e => typeof(TValue).IsAssignableFrom(e.FieldType) && e.Name == name);
return (TValue)field?.GetValue(instance);
}
public static TValue GetPropertyValue<TValue>(this object instance, string name)
{
var type = instance.GetType();
var field = type.GetProperties(BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance).FirstOrDefault(e => typeof(TValue).IsAssignableFrom(e.PropertyType) && e.Name == name);
return (TValue)field?.GetValue(instance);
}
Dim NewHandle As YourType = CType(Microsoft.VisualBasic.CallByName(ObjectThatContainsYourVariable, "YourVariableName", CallType), YourType)
Here is another way to find a nested property that doesn't require the string to tell you the nesting path. Credit to Ed S. for the single property method.
public static T FindNestedPropertyValue<T, N>(N model, string propName) {
T retVal = default(T);
bool found = false;
PropertyInfo[] properties = typeof(N).GetProperties();
foreach (PropertyInfo property in properties) {
var currentProperty = property.GetValue(model, null);
if (!found) {
try {
retVal = GetPropValue<T>(currentProperty, propName);
found = true;
} catch { }
}
}
if (!found) {
throw new Exception("Unable to find property: " + propName);
}
return retVal;
}
public static T GetPropValue<T>(object srcObject, string propName) {
return (T)srcObject.GetType().GetProperty(propName).GetValue(srcObject, null);
}
You never mention what object you are inspecting, and since you are rejecting ones that reference a given object, I will assume you mean a static one.
using System.Reflection;
public object GetPropValue(string prop)
{
int splitPoint = prop.LastIndexOf('.');
Type type = Assembly.GetEntryAssembly().GetType(prop.Substring(0, splitPoint));
object obj = null;
return type.GetProperty(prop.Substring(splitPoint + 1)).GetValue(obj, null);
}
Note that I marked the object that is being inspected with the local variable obj. null means static, otherwise set it to what you want. Also note that the GetEntryAssembly() is one of a few available methods to get the "running" assembly, you may want to play around with it if you are having a hard time loading the type.
Have a look at the Heleonix.Reflection library. You can get/set/invoke members by paths, or create a getter/setter (lambda compiled into a delegate) which is faster than reflection. For example:
var success = Reflector.Get(DateTime.Now, null, "Date.Year", out int value);
Or create a getter once and cache for reuse (this is more performant but might throw NullReferenceException if an intermediate member is null):
var getter = Reflector.CreateGetter<DateTime, int>("Date.Year", typeof(DateTime));
getter(DateTime.Now);
Or if you want to create a List<Action<object, object>> of different getters, just specify base types for compiled delegates (type conversions will be added into compiled lambdas):
var getter = Reflector.CreateGetter<object, object>("Date.Year", typeof(DateTime));
getter(DateTime.Now);
Although the original question was about how to get the value of the property using only a single string as the parameter, it makes a lot of sense here to use an Expression rather than simply a string to ensure that the caller never uses a hard coded property name. Here is a one line version with usage:
public static class Utils
...
public static TVal GetPropertyValue<T, TVal>(T t, Expression<Func<T, TVal>> x)
=> (TVal)((x.Body as MemberExpression)?.Member as PropertyInfo)!.GetValue(t);
...
var val = Utils.GetPropertyValue(foo, p => p.Bar);
Here is a slightly better version in terms of readability a error handling:
public static TVal GetPropertyValue<T, TVal>(T t, Expression<Func<T, TVal>> x)
{
var m = (x.Body as MemberExpression)?.Member;
var p = m as PropertyInfo;
if (null == p)
throw new ArgumentException($"Unknown property: {typeof(T).Name}.{(m?.Name??"???")}");
return (TVal)p.GetValue(t);
}
In short you pass in a lambda expression reading a property. The body of the lambda - the part on the right of the fat arrow - is a member expression from which you can get the member name and which you can cast to a PropertyInfo, provided the member is actually a Property and not, for instance, a method.
In the short version, the null forgiving operator - the ! in the expression - tells the compiler that the PropertyInfo will not be null. This is a big lie and you will get a NullReferenceException at runtime. The longer version gives you the name of the property if it manages to get it.
PS: Thanks to Oleg G. for the initial version of this code :)
shorter way ....
var a = new Test { Id = 1 , Name = "A" , date = DateTime.Now};
var b = new Test { Id = 1 , Name = "AXXX", date = DateTime.Now };
var compare = string.Join("",a.GetType().GetProperties().Select(x => x.GetValue(a)).ToArray())==
string.Join("",b.GetType().GetProperties().Select(x => x.GetValue(b)).ToArray());
jheddings and AlexD both wrote excellent answers on how to resolve property strings. I'd like to throw mine in the mix, since I wrote a dedicated library exactly for that purpose.
Pather.CSharp's main class is Resolver. Per default it can resolve properties, array and dictionary entries.
So, for example, if you have an object like this
var o = new { Property1 = new { Property2 = "value" } };
and want to get Property2, you can do it like this:
IResolver resolver = new Resolver();
var path = "Property1.Property2";
object result = r.Resolve(o, path);
//=> "value"
This is the most basic example of the paths it can resolve. If you want to see what else it can, or how you can extend it, just head to its Github page.
Here's what I got based on other answers. A little overkill on getting so specific with the error handling.
public static T GetPropertyValue<T>(object sourceInstance, string targetPropertyName, bool throwExceptionIfNotExists = false)
{
string errorMsg = null;
try
{
if (sourceInstance == null || string.IsNullOrWhiteSpace(targetPropertyName))
{
errorMsg = $"Source object is null or property name is null or whitespace. '{targetPropertyName}'";
Log.Warn(errorMsg);
if (throwExceptionIfNotExists)
throw new ArgumentException(errorMsg);
else
return default(T);
}
Type returnType = typeof(T);
Type sourceType = sourceInstance.GetType();
PropertyInfo propertyInfo = sourceType.GetProperty(targetPropertyName, returnType);
if (propertyInfo == null)
{
errorMsg = $"Property name '{targetPropertyName}' of type '{returnType}' not found for source object of type '{sourceType}'";
Log.Warn(errorMsg);
if (throwExceptionIfNotExists)
throw new ArgumentException(errorMsg);
else
return default(T);
}
return (T)propertyInfo.GetValue(sourceInstance, null);
}
catch(Exception ex)
{
errorMsg = $"Problem getting property name '{targetPropertyName}' from source instance.";
Log.Error(errorMsg, ex);
if (throwExceptionIfNotExists)
throw;
}
return default(T);
}
Here is my solution. It works also with COM objects and allows to access collection/array items from COM objects.
public static object GetPropValue(this object obj, string name)
{
foreach (string part in name.Split('.'))
{
if (obj == null) { return null; }
Type type = obj.GetType();
if (type.Name == "__ComObject")
{
if (part.Contains('['))
{
string partWithoundIndex = part;
int index = ParseIndexFromPropertyName(ref partWithoundIndex);
obj = Versioned.CallByName(obj, partWithoundIndex, CallType.Get, index);
}
else
{
obj = Versioned.CallByName(obj, part, CallType.Get);
}
}
else
{
PropertyInfo info = type.GetProperty(part);
if (info == null) { return null; }
obj = info.GetValue(obj, null);
}
}
return obj;
}
private static int ParseIndexFromPropertyName(ref string name)
{
int index = -1;
int s = name.IndexOf('[') + 1;
int e = name.IndexOf(']');
if (e < s)
{
throw new ArgumentException();
}
string tmp = name.Substring(s, e - s);
index = Convert.ToInt32(tmp);
name = name.Substring(0, s - 1);
return index;
}
Whenever you want to loop over all properties in on an object and then use each value of the property must use this piece of code:
foreach (var property in request.GetType().GetProperties())
{
var valueOfProperty = property.GetValue(properties, null);
}
I have a C# class called "SmallClass".
I have an existing list myList containing objects of type "SmallClass"
I want a deep clone of the list "myList". That is, deep Clone the containing list and deep clone the objects contained in the list.
How should I do this.
public class SmallClass: ICloneable {
public string str1;
public string str2;
public string str3;
public SmallClass Clone() //This just deep clones 1 object of type "SmallClass"
{
MemoryStream m = new MemoryStream();
BinaryFormatter b = new BinaryFormatter();
b.Serialize(m, this);
m.Position = 0;
return (SRO)b.Deserialize(m);
}
public override equals(Object a)
{
return Object.Equals(this.str1 && a.str1);
}
}
public class AnotherClass
{
SomeCode();
List<SmallClass> myList = new List<SmallList>(); //myList is initialized.
// NOW I want to deep clone myList. deep Clone the containing list and deep clone the objects contained in the list.
List<SmallClass> newList = new List<SmallClass>();
foreach(var item in myList)
{
newList.Add((SmallClass)item.Clone());
}
}
Warning: The BinaryFormatter type is dangerous when used with untrusted input. Whilst the usage below should be safe, Microsoft recommend avoiding BinaryFormatter altogether due to its potential for misuse, and will remove it from .NET 7–8. Consider using another serializer or approach for your deep clones.
First off, you can define a utility method for deep-cloning any object (root):
public static T DeepClone<T>(T obj)
{
using (var stream = new MemoryStream())
{
var formatter = new BinaryFormatter();
formatter.Serialize(stream, obj);
stream.Position = 0;
return (T)formatter.Deserialize(stream);
}
}
If you want to deep-clone myList, all you need to do is pass it as parameter to the method above:
List<SmallClass> myListClone = DeepClone(myList);
The most important consideration you need to pay attention to is that all your classes must be marked as serializable, typically through the [SerializableAttribute].
[SerializableAttribute]
public class SmallClass
{
// …
}
Your SmallClass needs to implement the ICloneable interface. Then copy every element using the Clone() method.
List<SmallClass> newList = new List<SmallClass>();
foreach(var item in myList)
{
newList.Add((SmallClass)item.Clone());
}
There are a few ways of creating a deep copy which include serialization and using Object.MemberwiseClone Method (). Since an example of using serialization is already available here, I have an approach with using "MemberwiseClone".
NOTES: Recursive, Platform: .NETStandard2.0
/// <summary>
/// Returns a deep copy of an object.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="source"></param>
/// <returns></returns>
public static T DeepClone<T>(this T source) where T : class
{
if(source == null) return null;
if(source is ICollection<object> col)
{
return (T)DeepCloneCollection(col);
}
else if(source is IDictionary dict)
{
return (T)DeepCloneDictionary(dict);
}
MethodInfo method = typeof(object).GetMethod("MemberwiseClone", BindingFlags.NonPublic | BindingFlags.Instance);
T clone = (T)method.Invoke(source, null);
foreach(FieldInfo field in source.GetType().GetRuntimeFields())
{
if(field.IsStatic) continue;
if(field.FieldType.GetTypeInfo().IsPrimitive) continue;
object sourceValue = field.GetValue(source);
field.SetValue(clone, DeepClone(sourceValue));
}
return clone;
}
private static ICollection<object> DeepCloneCollection(ICollection<object> col)
{
object[] arry = (object[])Activator.CreateInstance(col.GetType(), new object[] { col.Count });
for(int i = 0; i < col.Count; i++)
{
object orig = col.ElementAt(i);
object cln = DeepClone(orig);
arry[i] = cln;
}
return arry;
}
private static IDictionary DeepCloneDictionary(IDictionary dict)
{
IDictionary clone = (IDictionary)Activator.CreateInstance(dict.GetType());
foreach(object pair in dict)
{
object key = pair.GetValueOf("Key");
object original = pair.GetValueOf("Value");
clone.Add(key, original.DeepClone());
}
return clone;
}
public static dynamic GetValueOf<T>(this T value, string property)
{
PropertyInfo p = value.GetType().GetTypeInfo().GetProperty(property);
if(p != null && p.CanRead)
{
dynamic val = p.GetValue(value);
return val;
}
return Activator.CreateInstance(p.PropertyType); //Property does not have value, return default
}
I am using very similar loops to iterate all public fields and properties of any passed object. I determine if the field/property is decorated with a particular custom attribute; if so, an action is performed on the value of the field or property. Two loops are necessary because the method to get a field value is different from the method to get a property value.
// Iterate all public fields using reflection
foreach (FieldInfo fi in obj.GetType().GetFields())
{
// Determine if decorated with MyAttribute.
var attribs = fi.GetCustomAttributes(typeof(MyAttribute), true);
if (attribs.Length == 1)
{
// Get value of field.
object value = fi.GetValue(obj);
DoAction(value);
}
}
// Iterate all public properties using reflection
foreach (PropertyInfo pi in obj.GetType().GetProperties())
{
// Determine if decorated with MyAttribute.
var attribs = pi.GetCustomAttributes(typeof(MyAttribute), true);
if (attribs.Length == 1)
{
// Get value of property.
object value = pi.GetValue(obj, null);
DoAction(value);
}
}
I would like to place the loop in a single, common method so that I can instead write, more simply:
DoEachMember(obj.GetType().GetFields());
DoEachMember(obj.GetType().GetProperties());
This requires DoEachMember() to accept the MemberInfo type (which is the parent type of both FieldInfo and PropertyInfo). The problem is there is no GetValue method in the MemberInfo class. Both FieldInfo and PropertyInfo use different methods to get the field/property value:
public void DoEachMember(MemberInfo mi, object obj)
{
foreach (MemberInfo mi in obj.GetType().GetProperties())
{
object value mi.GetValue(obj); // NO SUCH METHOD!
}
}
Thus, I declare a delegate to utilize inside the loop which takes a MemberInfo and returns the value of that member as an object:
// Delegate to get value from field or property.
delegate object GetValue(MemberInfo mi, object obj);
The Question
How can I detect the type of the objects in the members[] array, in order to define the delegate used inside the loop? Currently, I am using the first element of the array, members[0]. Is this a good design?
public void DoEachMember(MemberInfo[] members, object obj)
{
// Protect against empty array.
if (members.Length == 0) return;
GetValue getValue; // define delegate
// First element is FieldInfo
if (members[0] as FieldInfo != null)
getValue = (mi, obj) => ((FieldInfo)mi).GetValue(obj);
// First element is PropertyInfo
else if (members[0] as PropertyInfo != null)
getValue = (mi, obj) => ((PropertyInfo)mi).GetValue(obj, null);
// Anything else is unacceptable
else
throw new ArgumentException("Must be field or property.");
foreach (MemberInfo mi in members)
{
// Determine if decorated with MyAttribute.
var attribs = mi.GetCustomAttributes(typeof(MyAttribute), true);
if (attribs.Length == 1)
{
object value = getValue(mi, obj);
DoStuff(value);
}
}
}
Alternatively, I could detect the type upon each iteration, but there should be no reason individual array members will ever differ:
foreach (MemberInfo mi in members)
{
// ...
object value;
if ((var fi = mi as FieldInfo) != null)
value = fi.GetValue(obj);
else if ((var pi = mi as PropertyInfo) != null)
value = pi.GetValue(obj, null);
else
throw new ArgumentException("Must be field or property.");
DoStuff(value);
}
You could project to the object values first and then work on those in your loop. Your whole code could be boiled down to this (plus your loop):
/// <summary>
/// Gets the value of all the fields or properties on an object that are decorated with the specified attribute
/// </summary>
private IEnumerable<object> GetValuesFromAttributedMembers<TAttribute>(object obj)
where TAttribute : Attribute
{
var values1 = obj.GetType().GetFields()
.Where(fi => fi.GetCustomAttributes(typeof(TAttribute), true).Any())
.Select(fi => fi.GetValue(obj));
var values2 = obj.GetType().GetProperties()
.Where(pi => pi.GetCustomAttributes(typeof(TAttribute), true).Any())
.Select(pi => pi.GetValue(obj, null));
return values1.Concat(values2);
}
Your current code mixes two concerns: finding the values and doing something with them. It would be cleaner to separate those concerns. The above LINQ could be placed in one method that fetches all values from a class that are in fields or properties that match a given attribute and another than is just a loop doing the work on whatever it is passed.
Not as clean but sticking with your original goal you could do this and pass in a delegate appropriate to the type of the MemberInfo you are retrieving:-
public void DoEachMember<TAttribute, TMembertype>(IEnumerable<TMembertype> members,
Func<TMembertype, object> valueGetter)
where TMembertype : MemberInfo
{
foreach (var mi in members)
{
if (mi.GetCustomAttributes(typeof(TAttribute), true).Any())
{
// Get value of field.
object value = valueGetter(mi);
DoAction(value);
}
}
}
You should use generics:
public void DoEachMember<T>(T[] members, object obj) where T: MemberInfo
{
}
Inside, test what T is, and decide which method to call based on that:
if(typeof(T)==PropertyInfo.GetType()) ...
You can obviously do the check just once, and not every iteration.
I approached this by wrapping MemberInfo in an interface like so:
public interface IMemberInfo
{
MemberInfo Wrapped { get; }
object GetValue( object obj );
void SetValue( object obj, object value );
}
internal abstract class MemberInfoWrapper : IMemberInfo
{
protected readonly MemberInfo MemberInfo;
public MemberInfoWrapper( MemberInfo memberInfo )
{
MemberInfo = memberInfo;
}
public abstract object GetValue( object obj );
public abstract void SetValue( object obj, object value );
public virtual MemberInfo Wrapped
{
get { return MemberInfo; }
}
}
internal class PropertyInfoWrapper : MemberInfoWrapper
{
public PropertyInfoWrapper( MemberInfo propertyInfo ) : base( propertyInfo )
{
Debug.Assert( propertyInfo is PropertyInfo );
}
public override object GetValue( object obj )
{
return ( (PropertyInfo)MemberInfo ).GetValue( obj, null );
}
public override void SetValue( object obj, object value )
{
( (PropertyInfo)MemberInfo ).SetValue( obj, value, null );
}
}
internal class FieldInfoWrapper : MemberInfoWrapper
{
public FieldInfoWrapper( MemberInfo fieldInfo ) : base( fieldInfo )
{
Debug.Assert( fieldInfo is FieldInfo );
}
public override object GetValue( object obj )
{
return ( (FieldInfo)MemberInfo ).GetValue( obj );
}
public override void SetValue( object obj, object value )
{
( (FieldInfo)MemberInfo ).SetValue( obj, value );
}
}
And a factory:
internal static class MemberInfoWrapperFactory
{
public static IMemberInfo CreateWrapper( this MemberInfo memberInfo )
{
switch ( memberInfo.MemberType )
{
case MemberTypes.Property:
return new PropertyInfoWrapper( memberInfo );
case MemberTypes.Field:
return new FieldInfoWrapper( memberInfo );
default:
return null;
}
}
}
Given this, you can, in your method:
// Iterate all public members using reflection
foreach (MemberInfo mi in obj.GetType().GetMembers().Where(x => x is PropertyInfo || x is FieldInfo))
{
// Determine if decorated with MyAttribute.
var attribs = mi.GetCustomAttributes(typeof(MyAttribute), true);
if (attribs.Length == 1)
{
// Get value of property.
object value = mi.CreateWrapper().GetValue(obj, null);
DoAction(value);
}
}
Try this:
var areProperties = members.All(m => m is PropertyInfo);
var areFields = members.All(m => m is FieldInfo);
areProperties will be true only if all items in the members[] array are PropertyInfo objects.
You can do something like this, if you're using C# 4.0
public void DoEachMember(MemberInfo[] members, object obj)
{
var properties = new List<dynamic>(); //dynamic objects list
properties.AddRange(members) ; // add all members to list of dynamics
foreach(dynamic d in porperties) //iterate over collection
{
var attribs = d.GetCustomAttributes(typeof(MyAttribute), true); //call dynamicaly
if (attribs.Length == 1)
{
// Get value of property.
object value = d.GetValue(obj, null); //call dynamically
DoAction(value);
}
}
}
Code becomes very short and straightforward.
Should work.
Good luck
Background
Working in .NET 2.0 Here, reflecting lists in general. I was originally using t.IsAssignableFrom(typeof(IEnumerable)) to detect if a Property I was traversing supported the IEnumerable Interface. (And thus I could cast the object to it safely)
However this code was not evaluating to True when the object is a BindingList<T>.
Next
I tried to use t.IsSubclassOf(typeof(IEnumerable)) and didn't have any luck either.
Code
/// <summary>
/// Reflects an enumerable (not a list, bad name should be fixed later maybe?)
/// </summary>
/// <param name="o">The Object the property resides on.</param>
/// <param name="p">The Property We're reflecting on</param>
/// <param name="rla">The Attribute tagged to this property</param>
public void ReflectList(object o, PropertyInfo p, ReflectedListAttribute rla)
{
Type t = p.PropertyType;
//if (t.IsAssignableFrom(typeof(IEnumerable)))
if (t.IsSubclassOf(typeof(IEnumerable)))
{
IEnumerable e = p.GetValue(o, null) as IEnumerable;
int count = 0;
if (e != null)
{
foreach (object lo in e)
{
if (count >= rla.MaxRows)
break;
ReflectObject(lo, count);
count++;
}
}
}
}
The Intent
I want to basically tag lists i want to reflect through with the ReflectedListAttribute and call this function on the properties that has it. (Already Working)
Once inside this function, given the object the property resides on, and the PropertyInfo related, get the value of the property, cast it to an IEnumerable (assuming it's possible) and then iterate through each child and call ReflectObject(...) on the child with the count variable.
When you do the as IEnumerable and the variable is not null you know that it does implement IEnumerable interface.
You don´t need the code:
Type t = p.PropertyType;
//if (t.IsAssignableFrom(typeof(IEnumerable)))
if (t.IsSubclassOf(typeof(IEnumerable)))
{
This would be enough:
public void ReflectList(object o, PropertyInfo p, ReflectedListAttribute rla)
{
IEnumerable e = p.GetValue(o, null) as IEnumerable;
int count = 0;
if (e != null)
{
foreach (object lo in e)
{
if (count >= rla.MaxRows)
break;
ReflectObject(lo, count);
count++;
}
}
}
From MSDN
The IsSubclassOf method cannot be used
to determine whether an interface
derives from another interface, or
whether a class implements an
interface Use the GetInterface method for that purpose
Also your implementation of IsAssignableFrom is wrong, you should use it like this:
typeof(IEnumerable).IsAssignableFrom(t)
This should return true if IEnumerable is implemented by t..
Why do you the if-statement at all?
You already did a var e = ... as IEnumerable and afterwards just check if is not null.
Isn't that enough?
These work. :)
A List is extended Collection. So, the tests are different for them. A Dictionary has got two internal containers. Hence one test for the same.
public static bool IsList(object obj)
{
System.Collections.IList list = obj as System.Collections.IList;
return list != null;
}
public static bool IsCollection(object obj)
{
System.Collections.ICollection coll = obj as System.Collections.ICollection;
return coll != null;
}
public static bool IsDictionary(object obj)
{
System.Collections.IDictionary dictionary = obj as System.Collections.IDictionary;
return dictionary != null;
}
Usage example -
if (IsDictionary(fieldObject)) //key-value type collection?
{
System.Collections.IDictionary dictionary = fieldObject as System.Collections.IDictionary;
foreach (object _ob in dictionary.Values)
{
//do work
}
// dictionary.Clear();
}
else //normal collection
{
if (IsCollection(fieldObject))
{
System.Collections.ICollection coll = fieldObject as System.Collections.ICollection;
foreach (object _ob in coll)
{
//do work
}
if (IsList(fieldObject))
{
//System.Collections.IList list = fieldObject as System.Collections.IList;
//list.Clear(); // <--- List's function, not Collection's.
}
}
}