使用反射在ValueTypes上设置属性(结构) [英] Using reflection to Set properties on ValueTypes (Structure)
本文介绍了使用反射在ValueTypes上设置属性(结构)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有谁知道在这种特殊情况下,如何使用反射在ValueTypes上设置一些值?
情况是:
Hi,
Does anyone know how to use reflection to Set some values on ValueTypes in this particular case Structure?
The scenario is:
public structure my_struct {
public string name;
}
class B {
public my_struct c_struct;
}
那么如何在对象B中设置名称变量my_struct呢?
谢谢.
So how to set the name variable my_struct in object B?
Thanks.
推荐答案
使用此:
Use this :
object obj = Activator.CreateInstance(typeof(Person));
FieldInfo finfo = obj.GetType().GetField("c_struct");
finfo.SetValue(obj, structobj);
如果还想创建struct对象,请使用类似的方法:
if you want to create object of struct as well, use similarly :
object value = Activator.CreateInstance(typeof(my_struct));
FieldInfo field = type.GetField("name");
field.SetValue(value, "abhishek", null);
:rose:
:rose:
这篇关于使用反射在ValueTypes上设置属性(结构)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文