【发布时间】:2017-03-23 21:05:00
【问题描述】:
我有一个名为 Student 的班级:
public class Student
{
public int ID { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
当我创建 Student 类的实例时,它是 null 。
Student s = new Student();
" s.ID 为 null ,s.Name 为 null 并且 s.Age 为 null 。"
我想为 Student 类设置一个默认值,所以当我创建它的实例时:
Student s = new Student();
" s.ID = 1 , s.Name = Parsa , s.Age = 20 "
换句话说,我想更改属性 getter 的声明或实现或覆盖它。
我该怎么做?
更新
我知道我可以用 Static 类或定义 Constractor 来做到这一点,但我无权访问 Student 类,我想要它 Not-Static 。 我认为这个问题可以通过Reflection
来解决在此先感谢您。
【问题讨论】:
-
所以你不能为此使用
Student类的构造函数? -
不,因为我没有。我正在使用反射,我的类库用户定义了这个类。
-
为什么要为“反射”添加标签?您是否使用反射创建实例?
-
你应该提到你不能改变你的问题中的类声明。
-
为什么需要反思?是什么阻止您通过对象初始化器设置值?
标签: c# reflection orm