非法财产声明

时间:2016-02-11 15:55:26

标签: c# properties

我一直试图找出哪些是非法的:

public int MyProperty { get; set;}

public int MyProperty { get; protected set; }

public int MyProperty { get; }

public int MyProperty { get{ return 0; } }

public int MyProperty { get { return 0; } set { ; } }

他们都没有为我返回错误,但其中一个应该是非法的。有人可以告诉我哪一个以及为什么?

2 个答案:

答案 0 :(得分:3)

在C#6.0中所有都是合法的,在以前的版本中

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.Data.Linq.Mapping;

namespace QueryBuilder
{
    public partial class FrmQueryBuilder : Form
    {

        public FrmQueryBuilder();
        {
            InitializeComponent();
        }

        public void Form1_Load(object sender, EventArgs e)
        {
            PropertyInfo[] properties = typeof(DBClassDataContext).GetProperties();

            foreach (PropertyInfo property in properties)
            {
                if (property.PropertyType.IsClass)
                {
                    object[] attributes = property.PropertyType.GetGenericArguments()[0].GetCustomAttributes(typeof(TableAttribute), false);

                    if (attributes.Length > 0)
                    {
                        tableCheckedListBox.Items.Add(property.Name);
                    }
                }
            }
        }

        private void tableCheckedListBox_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            PropertyInfo[] tableColumns1 = typeof(TABLENAMEONE).GetProperties();
            PropertyInfo[] tableColumns2 = typeof(TABLENAMETWO).GetProperties();
            PropertyInfo[] tableColumns3 = typeof(TABLENAMETHREE).GetProperties();

            foreach (PropertyInfo columns1 in tableColumns1)
            {
                if (columns1.PropertyType.IsClass)
                {
                    object[] colAttrib1 = columns1.PropertyType.GetGenericArguments()[0].GetCustomAttributes(typeof(ColumnAttribute), false);

                    if (colAttrib1.Length > 0)
                    {
                        tableCheckedListBox.Items.Add(columns1.Name);
                    }
                }
            }

            foreach (PropertyInfo columns2 in tableColumns2)
            {
                if (columns2.PropertyType.IsClass)
                {
                    object[] colAttrib2 = columns2.PropertyType.GetGenericArguments()[0].GetCustomAttributes(typeof(ColumnAttribute), false);

                    if (colAttrib2.Length > 0)
                    {
                        tableCheckedListBox.Items.Add(columns2.Name);
                    }
                }
            }

            foreach (PropertyInfo columns3 in tableColumns3)
            {
                if (columns3.PropertyType.IsClass)
                {
                    object[] colAttrib3 = columns3.PropertyType.GetGenericArguments()[0].GetCustomAttributes(typeof(ColumnAttribute), false);

                    if (colAttrib3.Length > 0)
                    {
                        tableCheckedListBox.Items.Add(columns3.Name);
                    }
                }
            }
        }

        private void columnCheckedListBox_ItemCheck(object sender, ItemCheckEventArgs e)
        {

        }
    }
}

非法 - 您无法指定(即 public int MyProperty { get; } )返回的值。但是,在 C#6.0 中,您可以输入

set

或在构造函数中赋值

  public int MyProperty { get; } = 0;

答案 1 :(得分:-1)

public int MyProperty { get; } 

是非法的。

你可以简单地写

public int MyProperty { get; private set; }

归档