我在类program1中有一个返回类型erorr aking for return type"公共客户()" erorr

时间:2015-09-17 13:43:51

标签: c#

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 个答案:

答案 0 :(得分: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);
    }
  }