I have a List<T> of element and a PropertyInfo with a list of the property of T.
How can I identify a single property of an element in a loop ?
Ideally :
List<T>[i].PropertyInfo[y].Name
If i understood the question currently and you're looking to match the two collections, then you could use the type of the property info(Has a property type Property) and the typeof(T).
For efficiency i would recommend creating a dynamic method that would be cached for future usage.
If you are looking for the type of the properties, you must use the property:
PropertyInfo.PropertyType
I solved the problem using a solution like the one in the post "Get property value from string using reflection in C#".
Related
It is possible to override the TryGetIndex method of a dynamic object to access the dynamic object properties by index however I am dealing with an Expandoobject (of the System.dynamic namespace) which you can't inherit from. Is there a way around this? Thanks
ExpandoObject is nothing but a fancy IDictionary which leverages the DLR.
There is no way you can access a IDictionary<TKey,TValue> via index. You may find ElementAt method of linq useful, but it is not. There is no ordering in dictionary, You can read more about hashtable datastructure(Dictionary is also a hashtable).
For accessing dictionary via index you may use OrderedDictionary. One disadvantage is that is is not generic.
Know more about issues when accessing elements via index from a Dictionary
I have produced a custom attribute which simply marks a classes property as being the 'display name'. What I would like to do is find the property within a given class which has been marked with my attribute. As far as I know, the only way I can do this is to loop through each property (via reflection) and check what attributes it has assigned. Is there any easier/quicker way than this?
foreach (PropertyInfo property in myClassProperties)
{
//Get the alias attributes.
object[] attr=
property.GetCustomAttributes(typeof(DisplayField), true);
if(attr.Count() > 0)
{
// This is a display field!
}
}
Thanks
Well, it's slightly simpler than checking all its attributes to find the one you want - you can ask any member whether it has a particular attribute using IsDefined:
var properties = type.GetProperties()
.Where(p => p.IsDefined(typeof(MyAttribute), false));
Obviously you can cache that result on a per-type basis if you're going to use it multiple times.
As far as I know, the only way I can do this is to loop through each property (via reflection) and check what attributes it has assigned.
That's exactly the way to do it. Attributes are metadata which is baked into the assembly at compile time. In order to access them at runtime you need Reflection.
the only quicker way that I'm aware of is to create a dictionary either statically or in a singleton... so that subsequent visits are faster. I do this caching sometimes, but I do exactly as you outline above for the retrieve attribute functionality.
Lately I have been using reflection to work in my project, and I have the current question.
While in Type.GetProperties(Flags), we can filter the properties we get using 'Flags'; in TypeDescriptor.GetProperties(), we don't.
In type.GetProperties I can filter to get only properties not inherited.
Is it possible to do the same with TypeDescriptor.GetProperties() (only properties not inherited)?
Thank you
No, you can't.
The TypeDescriptor.GetProperties() is used to get PropertyDescriptor instances with possibility to filter using specific Attributes.
The Type.GetProperties() is used to get PropertyInfo instances with possibility to filter using specific BindingFlags.
Hey guys. I have the following situation.
I want to use a TypeDescriptor to get the properties of a certain type. The type's depth in the inheritance hierarchy may vary. I only want to get the properties declared in the type itself and not in its parents (base). The problem is that when I call TypeDescriptor.GetProperties() it would return everything declared up the inheritance hierarchy up to Object.
I only saw that I can filter the output by Attributes, but I don't want to add another attribute to the properties in my types just for this. Getting them through reflection and not using TypeDescriptor would do what I want, but is not an option for me, because some of the properties will be added dynamically to the type at some point.
Any ideas? If the question is not clear I could provide an example.
You can filter the properties using the ComponentType property :
var properties = from p in TypeDescriptor.GetProperties(x).Cast<PropertyDescriptor>()
where p.ComponentType == x.GetType()
select p;
Can't you just modify the implementation of ICustomTypeDescriptor to reflect your desired behavior?
I have a name of a property and need to find its value within a Class, what is the fastest way of getting to this value?
I am making the assumption that you have the name of the property in runtime; not while coding...
Let's assume your class is called TheClass and it has a property called TheProperty:
object GetThePropertyValue(object instance)
{
Type type = instance.GetType();
PropertyInfo propertyInfo = type.GetProperty("TheProperty");
return propertyInfo.GetValue(instance, null);
}
I assume you mean you have the name of the property as a string. In this case, you need to use a bit of reflection to fetch the property value. In the example below the object containing the property is called obj.
var prop = obj.GetType().GetProperty("PropertyName");
var propValue = prop.GetValue(obj, null);
Hope that helps.
If you're interested in speed at runtime rather than development, have a look at Jon Skeet's Making reflection fly and exploring delegates blog post.
Just use the name of the property. If it is a nullable property (e.g. int ? property) use property.Value.
At runtime you can use reflection to get the value of the property.
Two caveats:
Obfuscation: An obfuscator may change
the name of the property, which
will break this functionality.
Refactoring: Using reflection in this
manner makes the code more difficult
to refactor. If you change the name
of the property, you may have to
search for instances where you use
reflection to get the property value
based upon name.