Click here to Skip to main content
15,938,448 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
Does anyone know how to use reflection to Set some values on ValueTypes in this particular case Structure?

The scenario is:
C#
public structure my_struct {
          public string name;
}

class B {
     public my_struct c_struct;
}

So how to set the name variable my_struct in object B?

Thanks.
Posted
Updated 5-Jul-10 2:33am
v2

1 solution

Use this :

object obj = Activator.CreateInstance(typeof(Person));
FieldInfo finfo = obj.GetType().GetField("c_struct");
finfo.SetValue(obj, structobj);



if you want to create object of struct as well, use similarly :

C#
object value = Activator.CreateInstance(typeof(my_struct));
        FieldInfo field = type.GetField("name");
        field.SetValue(value, "abhishek", null);


:rose:
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900