我有一个返回类型错误在这里类程序1返回类型"公共客户()“;错误

本文关键字:错误 返回类型 客户 在这里 有一个 程序 quot | 更新日期: 2023-09-27 18:13:13

using System;
using System.Reflection;
namespace Reflection
{
    class Program
    {
        private static void Main(string[] args)
        {
            Type t = Type.GetType("program.program1");
            Console.WriteLine(t.FullName);
            PropertyInfo[] p = t.GetProperties();
            foreach (PropertyInfo pr in p)
            {
                Console.WriteLine(pr.Name);
            }
        }
    }
    public class Program1
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public Customer(int ID, string Name)
        {
            this.Id = ID;
            this.Name = Name; 
        }
        public Customer()
        {
            this.Id = -1;
            this.Name = string.Empty;
        }
        public void PrintId()
        {
            Console.WriteLine(this.Id);
        }
        public void PrintName()
        {
            Console.WriteLine(this.Name);
        }
    }
}

我有一个返回类型错误在这里类程序1返回类型"公共客户()“;错误

构造函数的名字应该对应于类的名字,所以重命名

  public class Program1 {...

  // class "Customer"...
  public class Customer {
    public int Id { get; set; }
    public string Name { get; set; }
    // Constructor has the same name that the class it creates
    public Customer(int ID, string Name)
    {
       this.Id = ID;
       this.Name = Name; 
    }
    // Constructor "Customer" has the same name that the class it creates
    public Customer()
    {
        this.Id = -1;
        this.Name = string.Empty;
    }
    public void PrintId()
    {
        Console.WriteLine(this.Id);
    }
    public void PrintName()
    {
        Console.WriteLine(this.Name);
    }
  }