The following is an introduction to the setvalue method and an introduction to the error
Sets the attribute value of the specified object with the optional index value of the indexed attribute.
C#
public virtual void SetValue (object obj, object value, object[] index);
parameter
obj Object
The object whose attribute value will be set.
value Object
New property value.
index Object[]
Optional index value for indexing attributes. For non-indexed properties, the value should be null.
accomplish
SetValue(Object, Object, Object[])
abnormal
ArgumentException
The index array does not contain the required parameter type.
or The set value function for this property cannot be found.
or - value cannot be converted to a type of PropertyType.
TargetException
In the .NET or portable class library for Windows Store apps, catch Exception instead.
The object does not match the target type, or a certain property is an instance property but obj is null.
TargetParameterCountException
The number of parameters in index does not match the number of parameters used by the index attribute.
MethodAccessException
In the .NET or portable class library for Windows Store applications, instead catch the base class exception MemberAccessException.
Attempt to illegally access private or protected methods in a class.
TargetInvocationException
An error occurred while setting the property value. For example, the index value specified for an index property is out of range. The InnerException property indicates the cause of the error.
example
Dictionary<string, string> dic = new Dictionary<string, string>(); foreach (Control control in )//Get all values in the control and add to the dictionary { if (control is TextBox ||control is ComboBox ||control is DateTimePicker ) { (, ); } } PropertyInfo[] propertys = ().GetProperties(); foreach (PropertyInfo property in propertys)//Use propertyyinfo's mapping method to assign values to all properties of the entity class incomingnews. { for (int i = 0; i<; i++) { (incomingnews, dic[].ToString(), null); } }
I didn't get it before, but later I saw the error message "The index array does not contain the required parameter types.
or The set value function for this property cannot be found.
or value cannot be converted to a type of PropertyType. ”
I found that the entity class I want to assign is not an entity's attribute, but an entity's fields, so the setvalue method cannot catch the fields I want to assign, just set these fields to automatic properties.
Supplement: C# () The problem of using reflection to assign values to attributes of a generic object
Problem description:
In a generic method, after instantiating a generic object, use reflection to obtain all attributes in the generic, and then assign values to these attributes of the object. The breakpoint found that the generic object was instantiated, and there was no problem; there was no problem with the attribute value to be assigned, but after calling (), the value in the generic object could not be assigned.
Problem Solving:
When calling this generic method, the type passed in is a structure. . This method only supports class, not struct.
The above is personal experience. I hope you can give you a reference and I hope you can support me more. If there are any mistakes or no complete considerations, I would like to give you advice.