Questions tagged [propertyinfo]

237 questions
358
votes
12 answers

Setting a property by reflection with a string value

I'd like to set a property of an object through Reflection, with a value of type string. So, for instance, suppose I have a Ship class, with a property of Latitude, which is a double. Here's what I'd like to do: Ship ship = new Ship(); string value…
David Hodgson
  • 10,104
  • 17
  • 56
  • 77
340
votes
16 answers

Reflection - get attribute name and value on property

I have a class, lets call it Book with a property called Name. With that property, I have an attribute associated with it. public class Book { [Author("AuthorName")] public string Name { get; private set; } } In my main…
user619891
56
votes
2 answers

Is there a way to set properties on struct instances using reflection?

I'm trying to write some code that sets a property on a struct (important that it's a property on a struct) and it's failing: System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle(); PropertyInfo propertyInfo =…
Victor Chelaru
  • 4,491
  • 3
  • 35
  • 49
54
votes
7 answers

Get string name of property using reflection

There is a whole wealth of reflection examples out there that allow you to get either: All properties in a class A single property, provided you know the string name Is there a way (using reflection, TypeDescriptor, or otherwise) to get the string…
Joel B
  • 12,082
  • 10
  • 61
  • 69
47
votes
2 answers

PropertyInfo : is the property an indexer?

I have the following code : PropertyInfo[] originalProperties = myType.GetProperties(); I want to exclude from originalProperties all the indexers (myVar["key"] appears as property named "Item"). What is the proper way ? Exclude all properties…
JYL
  • 8,228
  • 5
  • 39
  • 63
39
votes
5 answers

How do you get the Value of a property from PropertyInfo?

I've got an object that has a collection of properties. When I get the specific entity I can see the field I'm looking for (opportunityid) and that it's Value attribute is the Guid for this opportunity. This is the value I want, but it won't always…
sr28
  • 4,728
  • 5
  • 36
  • 67
26
votes
6 answers

Finding the hosting PropertyInfo from the MethodInfo of getter/setter

I do some type analysis in runtime using Reflection. If I have a MethodInfo instance, how can I figure out if this is a "real" method or is a getter/setter method of a property? And if it is a property, how can I find the its hosting PropertyInfo…
Mr. Lame
  • 1,247
  • 1
  • 17
  • 27
25
votes
2 answers

Why is TargetInvocationException treated as uncaught by the IDE?

I have some code that is using reflection to pull property values from an object. In some cases the properties may throw exceptions, because they have null references, etc. object result; try { result = propertyInfo.GetValue(target,…
22
votes
4 answers

How to set Vaues to the Nested Property using C# Reflection.?

I am trying to set a value to a Nested Property of Class dynamically using reflection. Could anyone help me to do this. I am having a class Region like below. public class Region { public int id; public string name; public Country…
Sravan
  • 1,095
  • 4
  • 16
  • 27
20
votes
2 answers

Why are PropertyInfo SetValue and GetValue so slow?

Why is the PropertyInfo methods for getting and setting a property so slow? If I build a delegate using Reflection.Emit, it is much faster. Are they doing something important, so that the time they take can be justified? That is... am I missing…
Miguel Angelo
  • 23,796
  • 16
  • 59
  • 82
19
votes
1 answer

Get DisplayAttribute attribute from PropertyInfo

class SomeModel { [Display(Name = "Quantity Required")] public int Qty { get; set; } [Display(Name = "Cost per Item")] public int Cost { get; set; } } I'm trying to map the model into a list of { PropertyName, DisplayName } pairs,…
fearofawhackplanet
  • 52,166
  • 53
  • 160
  • 253
17
votes
4 answers

How to tell if a PropertyInfo is of a particular enum type?

I have the following code: public class DataReader where T : class { public T getEntityFromReader(IDataReader reader, IDictionary FieldMappings) { T entity = Activator.CreateInstance(); Type entityType =…
griegs
  • 22,624
  • 33
  • 128
  • 205
17
votes
4 answers

Property set method not found in a derived type

As disgussed in .NET Reflection set private property one can set a property with a private setter. But when the property is defined in a base class, System.ArgumentException is thrown : "Property set method not found". An example can be: using…
tafa
  • 7,146
  • 3
  • 36
  • 40
15
votes
2 answers

How do I determine if a property was overridden?

I am doing a project that where I need to register all the properties, because of the system being so huge it would require a lot of work to register all the properties that i want to be dependent for the purpose of Xaml. The goal is to find all…
Benjamin
  • 429
  • 4
  • 17
14
votes
3 answers

Setting value in an array via reflection

Is there a way to set a single value in an array property via reflection in c#? My property is defined like this: double[] Thresholds { get; set; } For "normal" properties I use this code to set it via reflection: PropertyInfo pi =…
Boris
  • 8,551
  • 25
  • 67
  • 120
1
2 3
15 16