c# 如何通过反射 获取\设置属性值

c# 如何通过反射 获取\设置属性值

//定义类
public class MyClass
{
public int Property1 { get; set; }
}
static void Main()
{
MyClass tmp_Class = new MyClass();
tmp_Class.Property1 = 2;
Type type = tmp_Class.GetType(); //获取类型
System.Reflection.PropertyInfo propertyInfo = type.GetProperty("Property1"); //获取指定名称的属性
int value_Old = (int)propertyInfo.GetValue(tmp_Class, null); //获取属性值
Console.WriteLine(value_Old);
propertyInfo.SetValue(tmp_Class, 5, null); //给对应属性赋值
int value_New = (int)propertyInfo.GetValue(tmp_Class, null);
Console.WriteLine(value_New);

}

时间: 2024-08-01 20:23:13

c# 如何通过反射 获取\设置属性值的相关文章

c# 如何通过反射 获取\设置属性值、

//定义类 public class MyClass{public int Property1 { get; set; }}static void Main(){MyClass tmp_Class = new MyClass();tmp_Class.Property1 = 2;Type type = tmp_Class.GetType(); //获取类型System.Reflection.PropertyInfo propertyInfo = type.GetProperty("Property

C#利用反射获取对象属性值

public static string GetObjectPropertyValue<T>(T t, string propertyname){     Type type = typeof(T); PropertyInfo property = type.GetProperty(propertyname); if (property == null) return string.Empty; object o = property.GetValue(t, null); if (o == n

用反射获取类属性值并且赋值

/** * * @projectname 项目名称: cms-interface * @packageclass 包及类名: com.jy.modules.utils.CreditUtil.java * @description 功能描述: Object 可以是任意对象在不确定对象的时候获取值设置值 * @author 作 者: zhouzhiwei * @param 参 数: @param Requestobj * @param 参 数: @param obj * @param 参 数: @r

反射获取对象属性值和字段值

ps:没什么技术含量,直接贴代码 public T GetFieldValue<T>(object obj, string name) { Type type = obj.GetType(); System.Reflection.FieldInfo fieldInfo = type.GetField(name); if (fieldInfo == null) { throw new MissingFieldException(name); } object objectValue = fiel

java 通过反射获取类属性结构,类方法,类父类及其泛型,类,接口和包

首先自定义三个类 package reflection1; public interface MtInterface { void info(); } package reflection1; import java.io.Serializable; public class Creature<T> implements Serializable { private char gender; public double weight; private void breath() { Syste

IE8和IE7如何获取background-position属性值

IE8和IE7如何获取background-position属性值:之所以要单独介绍一下如何获取background-position属性值,是因为在IE8和IE8以下浏览器中存在一定的兼容性问题,下面对此做一下简单介绍,希望能够给需要的朋友带来帮助.一.使用style方式:此方式只能够获取使用style方式定义的css样式属性,代码如下: <!DOCTYPE html> <html> <head> <meta charset="utf-8"&

反射原理读取对象属性并设置属性值

Dictionary<string, string> dicNodes = new Dictionary<string, string>(); foreach (XmlNode node in nodes.ChildNodes) { if (node.NodeType==XmlNodeType.Element) { dicNodes.Add(node.Attributes["name"].Value,node.Attributes["value&quo

JQ获取元素属性值

2019.10.27 更新 通过一段时间的学习发现下面为什么会报错了 因为我都是用原生JS代码创建的DOM对象,而attr方法是属于JQ对象才可使用的方法. 哈哈,自己也真是沙雕呀 ------------------------------------------------------------------------------------------------ 最近在学习JAVA Web,自己也是做个下列表左右选择的小案例. 获取某个元素的属性值一直以为是要调用atrr方法,不过好像

[转] C#反射设置属性值和获取属性值

/// /// 获取类中的属性值 /// /// /// /// public string GetModelValue(string FieldName, object obj) { try { Type Ts = obj.GetType(); object o = Ts.GetProperty(FieldName).GetValue(obj, null); string Value = Convert.ToString(o); if (string.IsNullOrEmpty(Value))