setvalue

SetValue on PropertyInfo instance error “Object does not match target type” c#

社会主义新天地 提交于 2019-11-29 05:43:12
Been using a Copy method with this code in it in various places in previous projects (to deal with objects that have same named properties but do not derive from a common base class or implement a common interface). New place of work, new codebase - now it's failing at the SetValue with "Object does not match target type" even on very simple examples... and it worked last week.... public static void Copy(object fromObj, object toObj) { Type fromObjectType = fromObj.GetType(); Type toObjectType = toObj.GetType(); foreach (System.Reflection.PropertyInfo fromProperty in fromObjectType

Objective-c: why private ivars are not hidden from the outside access when using KVC

孤街醉人 提交于 2019-11-29 02:20:54
After trying to access ivars using KVC, I have noticed that there was no protection on private and protected ivars. It doesn't matter what I put a in front of the ivar (private or protected keyword) - an ivar is always a public ivar when using KVC method "setValue". Here is my code where all of the seven ivars and properties are changeble outside the class instance: //************ interface file ***************// @interface MyClass : NSObject { @public NSNumber *public_num; @protected NSNumber *protected_num; @private NSNumber *private_num; NSNumber *private_property; } @property (retain)

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

Is there a way to create a delegate to get and set values for a FieldInfo?

主宰稳场 提交于 2019-11-28 05:26:08
For properties there are GetGetMethod and GetSetMethod so that I can do: Getter = (Func<S, T>)Delegate.CreateDelegate(typeof(Func<S, T>), propertyInfo.GetGetMethod()); and Setter = (Action<S, T>)Delegate.CreateDelegate(typeof(Action<S, T>), propertyInfo.GetSetMethod()); But how do I go about FieldInfo s? I am not looking for delegates to GetValue and SetValue (which means I will be invoking reflection each time) Getter = s => (T)fieldInfo.GetValue(s); Setter = (s, t) => (T)fieldInfo.SetValue(s, t); but if there is a CreateDelegate approach here? I mean since assignments return a value , can I

Reflection Performance - Create Delegate (Properties C#)

北慕城南 提交于 2019-11-27 18:23:03
I'm having performance problems with using reflection. So I decided to create delegates for the properties of my objects and so far got this: TestClass cwp = new TestClass(); var propertyInt = typeof(TestClass).GetProperties().Single(obj => obj.Name == "AnyValue"); var access = BuildGetAccessor(propertyInt.GetGetMethod()); var result = access(cwp); static Func<object, object> BuildGetAccessor(MethodInfo method) { var obj = Expression.Parameter(typeof(object), "o"); Expression<Func<object, object>> expr = Expression.Lambda<Func<object, object>>( Expression.Convert( Expression.Call( Expression

Is there a way to create a delegate to get and set values for a FieldInfo?

。_饼干妹妹 提交于 2019-11-27 00:55:58
问题 For properties there are GetGetMethod and GetSetMethod so that I can do: Getter = (Func<S, T>)Delegate.CreateDelegate(typeof(Func<S, T>), propertyInfo.GetGetMethod()); and Setter = (Action<S, T>)Delegate.CreateDelegate(typeof(Action<S, T>), propertyInfo.GetSetMethod()); But how do I go about FieldInfo s? I am not looking for delegates to GetValue and SetValue (which means I will be invoking reflection each time) Getter = s => (T)fieldInfo.GetValue(s); Setter = (s, t) => (T)fieldInfo.SetValue

What's the difference between Dependency Property SetValue() & SetCurrentValue()

泄露秘密 提交于 2019-11-26 22:22:22
The reason why I am asking this is because I was recommended by @Greg D (from this question ) to use SetCurrentValue() instead, but a look at the docs and didn't see whats the difference. Or whats does "without changing its value source" mean? SetValue() Sets the local value of a dependency property, specified by its dependency property identifier. SetCurrentValue() Sets the value of a dependency property without changing its value source. Kent Boogaart The MSDN link you provided says it quite well: This method is used by a component that programmatically sets the value of one of its own

JavaFX ComboBox change value causes IndexOutOfBoundsException

余生颓废 提交于 2019-11-26 11:34:58
问题 I want to include checks for my combobox to restrict \"access\" to some of the values. I could just remove those unaccessible items from the list, yes, but I\'d like the user to see the other options, even if he can\'t select them (yet). Problem: Selecting another value inside a changelistener causes an IndexOutOfBoundsException, and I have no idea why. Here is a SSCCE. It creates a ComboBox with Integer values, and the first one is selected on default. Then I tried to keep it very easy:

What&#39;s the difference between Dependency Property SetValue() & SetCurrentValue()

南楼画角 提交于 2019-11-26 09:09:02
问题 The reason why I am asking this is because I was recommended by @Greg D (from this question) to use SetCurrentValue() instead, but a look at the docs and didn\'t see whats the difference. Or whats does \"without changing its value source\" mean? SetValue() Sets the local value of a dependency property, specified by its dependency property identifier. SetCurrentValue() Sets the value of a dependency property without changing its value source. 回答1: The MSDN link you provided says it quite well:

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\