【问题标题】:Set property default value before instantiation在实例化之前设置属性默认值
【发布时间】: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 为 nulls.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


【解决方案1】:

您必须将字段初始化为您想要的默认值。例如:

class MyClass
{
    int value = 42;

    public int Value
    {
        get {return this.value;}
        set {this.value = value;}
    }
}

您可以使用 DefaultValue 属性来通知设计者这个非零默认值:

class MyClass
{
    int value = 42;

    [DefaultValue(42)]    
    public int Value
    {
        get {return this.value;}
        set {this.value = value;}
    }
}

【讨论】:

    【解决方案2】:

    由于您无权访问 Student 类,您只需将您的类包装到另一个具有获取和设置 Student 类属性的属性的类中,并在新的类构造函数中根据需要定义默认值。

    【讨论】:

    • 对Dll用户没用,我想用默认数据填充用户定义的类属性。
    • 能否尝试为每个属性添加“默认值属性”并通过反射获取?
    • 不完全是!定义属性的默认值(带反射),以便 dll 用户可以获取它。
    【解决方案3】:

    简单地创建一个分配了默认值的构造函数:

    public Student(){
       this.ID = 1;
       this.Name = "Some name";
       this.Age = 25;
    }
    

    【讨论】:

      【解决方案4】:
      {
          private int _id = 0;
          public int ID
          {
              get
              {
                  return _id;
              }
              set
              {
                  _id = value;
              }
          }
          private string _Name = string.Empty;
          public string Name 
          {
              get
              {
                  return _Name;
              }
              set
              {
                  _Name = value;
              }
          }
      
          private int _Age = 0;
          public int Age 
          {
              get
              {
                  return _Age;
              }
              set
              {
                  _Age = value;
              }
          }
      }
      

      【讨论】:

      • 为了记录,我更喜欢构造函数的答案。我只是想向他展示如何设置默认值。
      【解决方案5】:

      当我创建一个 Student 类的实例时,它是 null 。

      " s.ID 为 null ,s.Name 为 null 且 s.Age 为 null 。"

      首先,AgeID 不能为空,因为它们是值类型而不是引用类型。

      其次,成员变量不会返回任何有用的数据,因为属性未初始化,因此对于数字类型,它将为 0,而对于引用类型,它将为空。

      我想为 Student 类设置一个默认值,所以当我创建 它的实例:

      学生 s = new Student(); " s.ID = 1 , s.Name = Parsa , s.Age = 20 "

      我能想到三种解决方案:

      解决方案 1:对象初始化器

      Student student = new Student { ID = 1, Name = "Parsa", Age = 20 }; // from C# 3.0
      

      解决方案 2:自动属性初始化器

      public class Student{
           public int ID { get; set; } = 1; 
           public int Name { get; set; } = "Parsa"; // C# 6 or higher
           public int Age { get; set; } = 20;
      }
      

      解决方案 3:使用构造函数

      public class Student
      {
          public int ID { get; set; }
          public string Name { get; set; }
          public int Age { get; set; }
      
          public Student(int id, String name, int age){
             this.ID = id;
             this.Name = name;
             this.Age = age;
          }
      }
      

      这样称呼它:

      Student s = new Student(1,"Parsa",20);
      

      【讨论】:

      • 我知道这些,但不幸的是我不能很好地告诉我我的目标:(
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-01-06
      • 1970-01-01
      • 2010-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-07
      相关资源
      最近更新 更多