【问题标题】:C# Automatic Property as Method ParameterC# 自动属性作为方法参数
【发布时间】:2014-04-01 00:35:35
【问题描述】:

目前我有一个有很多自动属性类的类,如下所示。

class SQLiteTables
{
    public class tbl_account_codes
    {
        [PrimaryKey, AutoIncrement]
        public int ACCT_ID { get; set; }
        public string ACCT_CODE { get; set; }
        public string ACCT_DESC { get; set; }
        public string FUND_CODE { get; set; }
        public string ROR_FLAG { get; set; }
        public string OR_FLAG { get; set; }
        public string AR_FLAG { get; set; }
        public DateTime CREATED_DATE { get; set; }
        public string CREATED_BY { get; set; }
        public Nullable<DateTime> LAST_MODIFIED_DATE { get; set; }
        public string LAST_MODIFIED_BY { get; set; }
        public string ACCT_STATUS { get; set; }
    }

    public class tbl_ack_receipt
    {
        [PrimaryKey, AutoIncrement]
        public int AR_ID { get; set; }
        public string TPAY_RECEIPT_NO { get; set; }
        public string TPAY_SIG_ALGO { get; set; }
        public string BFNS_CODE { get; set; }
        public string TAXT_CODE { get; set; }
        public string ACCT_CODE { get; set; }
        public DateTime AR_PERIOD_COVERED { get; set; }
        public Nullable<int> AR_QUARTER { get; set; }
        public string AR_ASSESSMENT_NO { get; set; }
        public Nullable<DateTime> AR_DUE_DATE { get; set; }
        public string RFNP_CODE { get; set; }
        public string RFNP_OTHER { get; set; }
        public decimal AR_BASIC_TAX { get; set; }
        public decimal AR_SURCHARGE { get; set; }
        public decimal AR_INTEREST { get; set; }
        public decimal AR_TOTAL_DUE { get; set; }
        public decimal AR_TOTAL_PAID { get; set; }
        public string MPAY_CODE { get; set; }
        public string TYPEP_CODE { get; set; }
        public string AR_REMARKS { get; set; }
        public string AR_STATUS { get; set; }
        public decimal AR_COMPROMISE { get; set; }
    }

    public class tbl_agency_codes
    {
        [PrimaryKey, AutoIncrement]
        public int AGENCY_ID { get; set; }
        public string AGENCY_CODE { get; set; }
        public string AGENCY_DESC { get; set; }
        public DateTime CREATED_DATE { get; set; }
        public string CREATED_BY { get; set; }
        public Nullable<DateTime> LAST_MODIFIED_DATE { get; set; }
        public string LAST_MODIFIED_BY { get; set; }
        public string AGENCY_STATUS { get; set; }
    }
}

每次我想从类中检索特定属性时,我都需要这样做。

var qry = conn.Table<Tbl.tbl_agency_codes>().Where(x => x.AGENCY_CODE.StartsWith("0605"));

不用说,写几十个是很累的。所以我想知道是否可以简化它并制作一个类似的方法。

private void ThisMethod<T>(SomeProperty SomeProperty)
{
    var qry = conn.Table<T>().Where(x => x.SomeProperty.StartsWith("Something"));
}

这可能吗?

【问题讨论】:

  • 你可以使用反射
  • 你明白吗,如果重命名某些属性,你的代码会被无声地破坏?

标签: c# .net methods properties parameters


【解决方案1】:

方法:

private void ThisMethod<T>(Expression<Func<T, bool>> PredicateExp)
{
   var qry = conn().Table<T>().Where(PredicateExp);
}

用法:

ThisMethod<Tbl.tbl_agency_codes>(x => x.AGENCY_CODE.StartsWith("0605"));

【讨论】:

    【解决方案2】:

    是的,但有一些相当棘手的代码。

    您可以将属性的 name 作为字符串传递并将其反映出来,例如 x.GetType().Properties.Where(p=&gt;p.Name == &lt;prop name here&gt;).GetValue(x)

    或者你可以构建一个 lambda 并传入

    【讨论】:

      猜你喜欢
      • 2017-08-08
      • 2021-02-05
      • 2011-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      • 2016-07-13
      相关资源
      最近更新 更多