propertyinfo

Extension method to get property name

两盒软妹~` 提交于 2019-11-28 13:56:49
I have an extension method to get property name as public static string Name<T>(this Expression<Func<T>> expression) { MemberExpression body = (MemberExpression)expression.Body; return body.Member.Name; } I am calling it as string Name = ((Expression<Func<DateTime>>)(() => this.PublishDateTime)).Name(); This is working fine and returns me PublishDateTime as string. However I have an issue with the calling statement, it is looking too complex and I want some thing like this. this.PublishDateTime.Name() Can some one modify my extension method? Try this: public static string Name<T,TProp>(this T

Using reflection read properties of an object containing array of another object

北慕城南 提交于 2019-11-28 09:12:42
How can I read the properties of an object that contains an element of array type using reflection in c#. If I have a method called GetMyProperties and I determine that the object is a custom type then how can I read the properties of an array and the values within. IsCustomType is method to determine if the type is custom type or not. public void GetMyProperties(object obj) { foreach (PropertyInfo pinfo in obj.GetType().GetProperties()) { if (!Helper.IsCustomType(pinfo.PropertyType)) { string s = pinfo.GetValue(obj, null).ToString(); propArray.Add(s); } else { object o = pinfo.GetValue(obj,

Setting value in an array via reflection

可紊 提交于 2019-11-28 07:19:11
问题 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 = myObject.GetType().GetProperty(nameOfPropertyToSet); pi.SetValue(myObject, Convert.ChangeType(valueToSet, pi.PropertyType), null); How would I have to change this code to set the value in an array property at an arbitrary position? Thanks! BTW: I tried to use the

PropertyInfo : is the property an indexer?

[亡魂溺海] 提交于 2019-11-28 02:24:22
问题 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 where propInfo.Name == "Item" is not an option. 回答1: Call PropertyInfo.GetIndexParameters - if the returned array is empty, it's not an indexer. 回答2: Another option is to use: myType.GetProperties().Except(myType.GetDefaultMembers().OfType<PropertyInfo>

Using reflection read properties of an object containing array of another object

我与影子孤独终老i 提交于 2019-11-27 02:50:06
问题 How can I read the properties of an object that contains an element of array type using reflection in c#. If I have a method called GetMyProperties and I determine that the object is a custom type then how can I read the properties of an array and the values within. IsCustomType is method to determine if the type is custom type or not. public void GetMyProperties(object obj) { foreach (PropertyInfo pinfo in obj.GetType().GetProperties()) { if (!Helper.IsCustomType(pinfo.PropertyType)) {

Get string name of property using reflection

。_饼干妹妹 提交于 2019-11-26 22:03:49
There is a whole wealth of reflection examples out there that allow you to get either: 1. All properties in a class 2. A single property, provided you know the string name Is there a way (using reflection, TypeDescriptor, or otherwise) to get the string name of a property in a class at runtime, provided all I have is an instance of the class and property? EDIT I know that I can easily get all the properties in a class using reflection and then get the name of each property. What I'm asking for is a function to give me name of a property, provided I pass it the instance of the property. In

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

时间秒杀一切 提交于 2019-11-26 15:30:49
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 = typeof(System.Drawing.Rectangle).GetProperty("Height"); propertyInfo.SetValue(rectangle, 5, null); The Height value (as reported by the debugger) never gets set to anything - it stays at the default value of 0. I have done plenty of reflection on classes before and this has worked fine. Also, I know that when dealing with structs, you need to use FieldInfo.SetValueDirect if

Why is TargetInvocationException treated as uncaught by the IDE?

旧巷老猫 提交于 2019-11-26 14:29:02
问题 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, null); } catch (TargetInvocationException ex) { result = ex.InnerException.Message; } catch (Exception ex) { result = ex.Message; } Ultimately the code works correctly, however when I am running under the debugger: When the property throws an exception, the IDE

Get string name of property using reflection

假装没事ソ 提交于 2019-11-26 08:11:28
问题 There is a whole wealth of reflection examples out there that allow you to get either: 1. All properties in a class 2. A single property, provided you know the string name Is there a way (using reflection, TypeDescriptor, or otherwise) to get the string name of a property in a class at runtime, provided all I have is an instance of the class and property? EDIT I know that I can easily get all the properties in a class using reflection and then get the name of each property. What I\'m asking

Setting a property by reflection with a string value

六月ゝ 毕业季﹏ 提交于 2019-11-26 01:31:11
问题 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 = \"5.5\"; PropertyInfo propertyInfo = ship.GetType().GetProperty(\"Latitude\"); propertyInfo.SetValue(ship, value, null); As is, this throws an ArgumentException : Object of type \'System.String\' cannot be converted to type \'System.Double\