c# - Set Value of an Array at Index via reflection (not Property) -


i want set value of array of class

 obj[] 

via reflection!

e.g.

 obj[2] = aa; 

how can via reflection in csharp? can indexed properties easily, not array types!

not entirely sure asking, assuming have class this:

public class blah {     public int[] stuff; } 

then should work:

//some setup var obj = new blah(); obj.stuff = new int[] { 1, 2, 3, 4, 5 }; 

get field want manipulate:

var field = obj     .gettype()     .getfields()     .single(f => f.name == "stuff"); 

now can value, use array.setvalue change element:

var thearray = (array)field.getvalue(obj); //set element 1 100 thearray.setvalue(100, 1); 

Comments

Popular posts from this blog

android - Pass an Serializable object in AIDL -

How to provide Authorization & Authentication using Asp.net, C#? -

How to use Authorization & Authentication in Asp.net, C#? -